Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
utrack committed Jun 29, 2018
2 parents f7460e7 + af07d3d commit 2be1567
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/protoc-gen-goclay/genhandler/tpl_httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (c *{{ $svc.GetName }}_httpClient) {{ $m.GetName }}(ctx {{ pkg "context" }}
if err != nil {
return nil, {{ pkg "errors" }}Wrap(err, "can't initiate HTTP request")
}
req = req.WithContext(ctx)
req.Header.Add("Accept", m.ContentType())
Expand Down
4 changes: 3 additions & 1 deletion integration/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ VGO_BIN:=$(GOBIN)/vgo
GEN_CLAY_BIN:=$(CURDIR)/bin/protoc-gen-goclay
export GEN_CLAY_BIN

all: clean build test

# install vgo
$(VGO_BIN):
ifeq (${VGO_VERSION},master)
Expand Down Expand Up @@ -55,4 +57,4 @@ test: $(VGO_BIN) build
fi

clean:
@find */ -type f -name Makefile -execdir sh -c "make clean; echo ;" \;
@find */ -type f -name Makefile -execdir sh -c "make clean; echo ;" \;
58 changes: 58 additions & 0 deletions integration/http_headers_passthru/pb/strings/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,64 @@ func TestHTTPHeadersPass_genClient(t *testing.T) {
so.True(calledFunc)
}

// TestHTTPHeadersPass_genClient_outgoingContext tests that generated HTTP client
// passes headers from grpc.ToOutgoingContext to the request by default.
func TestHTTPHeadersPass_genClient_outgoingContext(t *testing.T) {
so := assert.New(t)
impl, ts := getTestSvc()
defer ts.Close()

tc := []a{
a{
Name: "User-Agent",
Values: []string{"Go-http-client/1.1"},
},
a{
Name: "Accept",
Values: []string{"application/json"},
},
}
pt := []a{
a{
Name: "X-Test-Passthrough",
Values: []string{"v1", "Value2", "3"},
},
}

calledFunc := false

impl.f = func(ctx context.Context, req *String) (*String, error) {
calledFunc = true
md, ok := metadata.FromIncomingContext(ctx)
so.True(ok)

for _, c := range tc {
got := md.Get(strings.ToLower(c.Name))
so.EqualValues(c.Values, got)
}

for _, c := range pt {
got := md.Get(strings.ToLower(c.Name))
so.EqualValues(c.Values, got)
}

return &String{}, nil
}

ctx := context.Background()

for _, c := range pt {
for i := range c.Values {
ctx = metadata.AppendToOutgoingContext(ctx, c.Name, c.Values[i])
}
}

cli := NewStringsHTTPClient(http.DefaultClient, ts.URL)
_, err := cli.ToLower(ctx, &String{})
so.Nil(err)
so.True(calledFunc)
}

func getTestSvc() (*StringsImplementation, *httptest.Server) {
mux := http.NewServeMux()
impl := NewStrings()
Expand Down
24 changes: 23 additions & 1 deletion transport/httpclient/requestmw.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type RequestMutator func(*http.Request) (*http.Request, error)
type ResponseMutator func(*http.Response) (*http.Response, error)

// DefaultRequestMutators are used for every outgoing request.
var DefaultRequestMutators = []RequestMutator{}
var DefaultRequestMutators = []RequestMutator{clientReqHeadersFromMD()}

// DefaultResponseMutators are used for every received response.
var DefaultResponseMutators = []ResponseMutator{}
Expand All @@ -95,3 +95,25 @@ func clientRspHeaderCopier(md *metadata.MD) ResponseMutator {
return rsp, nil
}
}

// clientReqHeadersFromMD pushes metadata from OutgoingContext to the
// request headers.
func clientReqHeadersFromMD() RequestMutator {
return func(req *http.Request) (*http.Request, error) {
fmt.Println("called")
ctxmd, ok := metadata.FromOutgoingContext(req.Context())
if !ok {
return req, nil
}

for k := range ctxmd {
vv := ctxmd.Get(k)
for i := range vv {
req.Header.Add(k, vv[i])
}
}

return req, nil
}

}

0 comments on commit 2be1567

Please sign in to comment.