-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add RequestData in Context option for HTTP client (#747)
Signed-off-by: Pablo Mercado <odacremolbap@gmail.com>
- Loading branch information
1 parent
77f73c2
commit 4b69880
Showing
4 changed files
with
281 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
Copyright 2021 The CloudEvents Authors | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package http | ||
|
||
import ( | ||
"context" | ||
|
||
nethttp "net/http" | ||
"net/url" | ||
) | ||
|
||
type requestKey struct{} | ||
|
||
// RequestData holds the http.Request information subset that can be | ||
// used to retrieve HTTP information for an incoming CloudEvent. | ||
type RequestData struct { | ||
URL *url.URL | ||
Header nethttp.Header | ||
RemoteAddr string | ||
Host string | ||
} | ||
|
||
// WithRequestDataAtContext uses the http.Request to add RequestData | ||
// information to the Context. | ||
func WithRequestDataAtContext(ctx context.Context, r *nethttp.Request) context.Context { | ||
if r == nil { | ||
return ctx | ||
} | ||
|
||
return context.WithValue(ctx, requestKey{}, &RequestData{ | ||
URL: r.URL, | ||
Header: r.Header, | ||
RemoteAddr: r.RemoteAddr, | ||
Host: r.Host, | ||
}) | ||
} | ||
|
||
// RequestDataFromContext retrieves RequestData from the Context. | ||
// If not set nil is returned. | ||
func RequestDataFromContext(ctx context.Context) *RequestData { | ||
if req := ctx.Value(requestKey{}); req != nil { | ||
return req.(*RequestData) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
Copyright 2021 The CloudEvents Authors | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package http | ||
|
||
import ( | ||
"context" | ||
nethttp "net/http" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
tMethod = nethttp.MethodPost | ||
) | ||
|
||
func TestWithRequest(t *testing.T) { | ||
testCases := map[string]struct { | ||
request *nethttp.Request | ||
|
||
expectedRequest *RequestData | ||
}{ | ||
"request": { | ||
request: newRequest("http://testhost:8080/test/path.json"), | ||
expectedRequest: &RequestData{ | ||
Host: "testhost:8080", | ||
URL: newURL("http://testhost:8080/test/path.json"), | ||
Header: nethttp.Header{}, | ||
}, | ||
}, | ||
"request with headers": { | ||
request: newRequest("http://testhost:8080/test/path.json", | ||
requestOptionAddHeader("key1", "value1"), | ||
requestOptionAddHeader("key2", "value2.1"), | ||
requestOptionAddHeader("key2", "value2.2"), | ||
), | ||
expectedRequest: &RequestData{ | ||
Host: "testhost:8080", | ||
URL: newURL("http://testhost:8080/test/path.json"), | ||
Header: nethttp.Header{ | ||
"Key1": []string{"value1"}, | ||
"Key2": []string{"value2.1", "value2.2"}, | ||
}, | ||
}, | ||
}, | ||
"request with host header": { | ||
request: newRequest("http://testhost:8080/test/path.json", | ||
requestOptionHostHeader("alternative.host"), | ||
), | ||
expectedRequest: &RequestData{ | ||
Host: "alternative.host", | ||
URL: newURL("http://testhost:8080/test/path.json"), | ||
Header: nethttp.Header{}, | ||
}, | ||
}, | ||
"request with remote address": { | ||
request: newRequest("http://testhost:8080/test/path.json", | ||
requestOptionRemoteAddr("requester.address"), | ||
), | ||
expectedRequest: &RequestData{ | ||
Host: "testhost:8080", | ||
URL: newURL("http://testhost:8080/test/path.json"), | ||
Header: nethttp.Header{}, | ||
RemoteAddr: "requester.address", | ||
}, | ||
}, | ||
"nil request": { | ||
request: nil, | ||
expectedRequest: nil, | ||
}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
ctx := WithRequestDataAtContext(context.TODO(), tc.request) | ||
|
||
req := RequestDataFromContext(ctx) | ||
assert.Equal(t, req, tc.expectedRequest) | ||
}) | ||
} | ||
} | ||
|
||
type requestOption func(*nethttp.Request) | ||
|
||
func newRequest(url string, opts ...requestOption) *nethttp.Request { | ||
r, err := nethttp.NewRequest(tMethod, url, nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, opt := range opts { | ||
opt(r) | ||
} | ||
|
||
return r | ||
} | ||
|
||
func requestOptionAddHeader(key, value string) requestOption { | ||
return func(r *nethttp.Request) { | ||
r.Header.Add(key, value) | ||
} | ||
} | ||
|
||
func requestOptionHostHeader(host string) requestOption { | ||
return func(r *nethttp.Request) { | ||
r.Host = host | ||
} | ||
} | ||
|
||
func requestOptionRemoteAddr(addr string) requestOption { | ||
return func(r *nethttp.Request) { | ||
r.RemoteAddr = addr | ||
} | ||
} | ||
|
||
func newURL(u string) *url.URL { | ||
parsed, err := url.Parse(u) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return parsed | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters