-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: HTTP headers in Error type #404
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
97e14d3
feat: adds HTTP response headers to Error type
karel-rehor e087f4d
feat: add http/Error to error Channel making Error.Header accessible
karel-rehor 8203776
docs: update ExampleWriteApi_errors to show use of Header field
karel-rehor a83a386
docs: updates CHANGELOG.md
karel-rehor 758784a
chore: improve Error.HeaderToString and cover with test.
karel-rehor 0da64b3
chore: better leverage http.CanonicalHeaderKey in Error.HeaderToString()
karel-rehor 4e3ff5f
test: refactor to error_test.go and remove commented lines
karel-rehor af74d7a
feat: create write/Error wrapper to better handle http/Error with Hea…
karel-rehor 5e83bf6
Merge branch 'master' into feat/httpErrorHeaders
karel-rehor 20a74f5
chore: remove case used in investigating http.Error and add test of h…
karel-rehor 767aeb8
Merge branch 'feat/httpErrorHeaders' of github.com:influxdata/influxd…
karel-rehor 580d1ea
docs: update ExampleWriteAPI_errors
karel-rehor 584c614
chore: refactor - rm write/Error, exec goimports, simplify return fro…
karel-rehor 40eaebb
test: use t.Run for data driven test, exec goimports
karel-rehor 5858f61
docs: update CHANGELOG.md
karel-rehor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,105 @@ | ||
// Copyright 2020-2024 InfluxData, Inc. All rights reserved. | ||
// Use of this source code is governed by MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
package http | ||
|
||
import ( | ||
"fmt" | ||
ihttp "net/http" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"testing" | ||
) | ||
|
||
func TestWriteErrorHeaderToString(t *testing.T) { | ||
header := ihttp.Header{ | ||
"Date": []string{"2024-08-07T12:00:00.009"}, | ||
"Content-Length": []string{"12"}, | ||
"Content-Type": []string{"application/json", "encoding UTF-8"}, | ||
"X-Test-Value1": []string{"SaturnV"}, | ||
"X-Test-Value2": []string{"Apollo11"}, | ||
"Retry-After": []string{"2044"}, | ||
"Trace-Id": []string{"123456789ABCDEF0"}, | ||
} | ||
|
||
err := Error{ | ||
StatusCode: ihttp.StatusBadRequest, | ||
Code: "bad request", | ||
Message: "this is just a test", | ||
Err: nil, | ||
RetryAfter: 2044, | ||
Header: header, | ||
} | ||
|
||
fullString := err.HeaderToString([]string{}) | ||
|
||
// write order is not guaranteed | ||
assert.Contains(t, fullString, "Date: 2024-08-07T12:00:00.009") | ||
assert.Contains(t, fullString, "Content-Length: 12") | ||
assert.Contains(t, fullString, "Content-Type: application/json") | ||
assert.Contains(t, fullString, "X-Test-Value1: SaturnV") | ||
assert.Contains(t, fullString, "X-Test-Value2: Apollo11") | ||
assert.Contains(t, fullString, "Retry-After: 2044") | ||
assert.Contains(t, fullString, "Trace-Id: 123456789ABCDEF0") | ||
|
||
filterString := err.HeaderToString([]string{"date", "trace-id", "x-test-value1", "x-test-value2"}) | ||
|
||
// write order will follow filter arguments | ||
assert.Equal(t, filterString, | ||
"Date: 2024-08-07T12:00:00.009\nTrace-Id: 123456789ABCDEF0\nX-Test-Value1: SaturnV\nX-Test-Value2: Apollo11\n", | ||
) | ||
assert.NotContains(t, filterString, "Content-Type: application/json") | ||
assert.NotContains(t, filterString, "Retry-After: 2044") | ||
} | ||
|
||
func TestErrorIfaceError(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
statusCode int | ||
err error | ||
code string | ||
message string | ||
expected string | ||
}{ | ||
{name: "TestNestedErrorNotNilCode0Message0", | ||
statusCode: 418, | ||
err: fmt.Errorf("original test message"), | ||
code: "", | ||
message: "", | ||
expected: "original test message"}, | ||
{name: "TestNestedErrorNotNilCodeXMessageX", | ||
statusCode: 418, | ||
err: fmt.Errorf("original test message"), | ||
code: "bad request", | ||
message: "is this a teapot?", | ||
expected: "original test message"}, | ||
{name: "TestNestedErrorNilCodeXMessageX", | ||
statusCode: 418, | ||
err: nil, | ||
code: "bad request", | ||
message: "is this a teapot?", | ||
expected: "bad request: is this a teapot?"}, | ||
{name: "TestNesterErrorNilCodeXMessage0", | ||
statusCode: 418, | ||
err: nil, | ||
code: "I'm a teapot", | ||
message: "", | ||
expected: "Unexpected status code 418"}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
err := Error{ | ||
StatusCode: test.statusCode, | ||
Code: test.code, | ||
Message: test.message, | ||
Err: test.err, | ||
RetryAfter: 0, | ||
Header: ihttp.Header{}, | ||
} | ||
assert.Equal(t, test.expected, err.Error()) | ||
}) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,10 @@ import ( | |
"fmt" | ||
"io" | ||
"math" | ||
ihttp "net/http" | ||
"net/http/httptest" | ||
"runtime" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
"testing" | ||
Comment on lines
8
to
17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. activated |
||
|
@@ -265,3 +268,47 @@ func TestFlushWithRetries(t *testing.T) { | |
// two remained | ||
assert.Equal(t, 2, len(service.Lines())) | ||
} | ||
|
||
func TestWriteApiErrorHeaders(t *testing.T) { | ||
calls := 0 | ||
var mu sync.Mutex | ||
server := httptest.NewServer(ihttp.HandlerFunc(func(w ihttp.ResponseWriter, r *ihttp.Request) { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
calls++ | ||
w.Header().Set("X-Test-Val1", "Not All Correct") | ||
w.Header().Set("X-Test-Val2", "Atlas LV-3B") | ||
w.Header().Set("X-Call-Count", strconv.Itoa(calls)) | ||
w.WriteHeader(ihttp.StatusBadRequest) | ||
_, _ = w.Write([]byte(`{ "code": "bad request", "message": "test header" }`)) | ||
})) | ||
defer server.Close() | ||
svc := http.NewService(server.URL, "my-token", http.DefaultOptions()) | ||
writeAPI := NewWriteAPI("my-org", "my-bucket", svc, write.DefaultOptions().SetBatchSize(5)) | ||
defer writeAPI.Close() | ||
errCh := writeAPI.Errors() | ||
var wg sync.WaitGroup | ||
var recErr error | ||
wg.Add(1) | ||
go func() { | ||
for i := 0; i < 3; i++ { | ||
recErr = <-errCh | ||
assert.NotNil(t, recErr, "errCh should not run out of values") | ||
assert.Len(t, recErr.(*http.Error).Header, 6) | ||
assert.NotEqual(t, "", recErr.(*http.Error).Header.Get("Date")) | ||
assert.NotEqual(t, "", recErr.(*http.Error).Header.Get("Content-Length")) | ||
assert.NotEqual(t, "", recErr.(*http.Error).Header.Get("Content-Type")) | ||
assert.Equal(t, strconv.Itoa(i+1), recErr.(*http.Error).Header.Get("X-Call-Count")) | ||
assert.Equal(t, "Not All Correct", recErr.(*http.Error).Header.Get("X-Test-Val1")) | ||
assert.Equal(t, "Atlas LV-3B", recErr.(*http.Error).Header.Get("X-Test-Val2")) | ||
} | ||
wg.Done() | ||
}() | ||
points := test.GenPoints(15) | ||
for i := 0; i < 15; i++ { | ||
writeAPI.WritePoint(points[i]) | ||
} | ||
writeAPI.waitForFlushing() | ||
wg.Wait() | ||
assert.Equal(t, calls, 3) | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.