Skip to content

Commit 8edd3bd

Browse files
authored
Make all header keys lower case and add Set utility (#27)
This change copies what Go `http.Header` does but uses lower case as the canonical form. Before this change, calling `nexus.Header{nexus.HeaderRequestTimeout: "1s"}` would produce an invalid header value.
1 parent ff95ed2 commit 8edd3bd

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

Diff for: nexus/api.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ const version = "v0.0.11"
2222

2323
const (
2424
// Nexus specific headers.
25-
headerOperationState = "Nexus-Operation-State"
26-
headerOperationID = "Nexus-Operation-Id"
27-
headerRequestID = "Nexus-Request-Id"
28-
headerLink = "Nexus-Link"
25+
headerOperationState = "nexus-operation-state"
26+
headerOperationID = "nexus-operation-id"
27+
headerRequestID = "nexus-request-id"
28+
headerLink = "nexus-link"
2929

3030
// HeaderRequestTimeout is the total time to complete a Nexus HTTP request.
31-
HeaderRequestTimeout = "Request-Timeout"
31+
HeaderRequestTimeout = "request-timeout"
3232
// HeaderOperationTimeout is the total time to complete a Nexus operation.
3333
// Unlike HeaderRequestTimeout, this applies to the whole operation, not just a single HTTP request.
34-
HeaderOperationTimeout = "Operation-Timeout"
34+
HeaderOperationTimeout = "operation-timeout"
3535
)
3636

3737
const contentTypeJSON = "application/json"
@@ -119,13 +119,19 @@ func isMediaTypeOctetStream(contentType string) bool {
119119

120120
// Header is a mapping of string to string.
121121
// It is used throughout the framework to transmit metadata.
122+
// The keys should be in lower case form.
122123
type Header map[string]string
123124

124125
// Get is a case-insensitive key lookup from the header map.
125126
func (h Header) Get(k string) string {
126127
return h[strings.ToLower(k)]
127128
}
128129

130+
// Set sets the header key to the given value transforming the key to its lower case form.
131+
func (h Header) Set(k, v string) {
132+
h[strings.ToLower(k)] = v
133+
}
134+
129135
func prefixStrippedHTTPHeaderToNexusHeader(httpHeader http.Header, prefix string) Header {
130136
header := Header{}
131137
for k, v := range httpHeader {

Diff for: nexus/api_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func TestAddLinksToHeader(t *testing.T) {
9999
Type: "url",
100100
}},
101101
output: http.Header{
102-
headerLink: []string{
102+
http.CanonicalHeaderKey(headerLink): []string{
103103
`<https://example.com/path/to/something?param=value>; type="url"`,
104104
},
105105
},
@@ -127,7 +127,7 @@ func TestAddLinksToHeader(t *testing.T) {
127127
},
128128
},
129129
output: http.Header{
130-
headerLink: []string{
130+
http.CanonicalHeaderKey(headerLink): []string{
131131
`<https://example.com/path/to/something?param=value>; type="url"`,
132132
`<https://foo.com/path/to/something?bar=value>; type="url"`,
133133
},
@@ -174,7 +174,7 @@ func TestGetLinksFromHeader(t *testing.T) {
174174
{
175175
name: "single link",
176176
input: http.Header{
177-
headerLink: []string{
177+
http.CanonicalHeaderKey(headerLink): []string{
178178
`<https://example.com/path/to/something?param=value>; type="url"`,
179179
},
180180
},
@@ -191,7 +191,7 @@ func TestGetLinksFromHeader(t *testing.T) {
191191
{
192192
name: "multiple links",
193193
input: http.Header{
194-
headerLink: []string{
194+
http.CanonicalHeaderKey(headerLink): []string{
195195
`<https://example.com/path/to/something?param=value>; type="url"`,
196196
`<https://foo.com/path/to/something?bar=value>; type="url"`,
197197
},
@@ -220,7 +220,7 @@ func TestGetLinksFromHeader(t *testing.T) {
220220
{
221221
name: "multiple links single header",
222222
input: http.Header{
223-
headerLink: []string{
223+
http.CanonicalHeaderKey(headerLink): []string{
224224
`<https://example.com/path/to/something?param=value>; type="url", <https://foo.com/path/to/something?bar=value>; type="url"`,
225225
},
226226
},
@@ -248,7 +248,7 @@ func TestGetLinksFromHeader(t *testing.T) {
248248
{
249249
name: "invalid header",
250250
input: http.Header{
251-
headerLink: []string{
251+
http.CanonicalHeaderKey(headerLink): []string{
252252
`<https://example.com/path?param=value> type="url"`,
253253
},
254254
},

Diff for: nexus/handle.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (h *OperationHandle[T]) GetResult(ctx context.Context, options GetOperation
9494
request.URL.RawQuery = ""
9595
}
9696

97-
response, err := h.sendGetOperationRequest(ctx, request)
97+
response, err := h.sendGetOperationRequest(request)
9898
if err != nil {
9999
if wait > 0 && errors.Is(err, errOperationWaitTimeout) {
100100
// TODO: Backoff a bit in case the server is continually returning timeouts due to some LB configuration
@@ -119,7 +119,7 @@ func (h *OperationHandle[T]) GetResult(ctx context.Context, options GetOperation
119119
}
120120
}
121121

122-
func (h *OperationHandle[T]) sendGetOperationRequest(ctx context.Context, request *http.Request) (*http.Response, error) {
122+
func (h *OperationHandle[T]) sendGetOperationRequest(request *http.Request) (*http.Response, error) {
123123
response, err := h.client.options.HTTPCaller(request)
124124
if err != nil {
125125
return nil, err

0 commit comments

Comments
 (0)