Skip to content

Commit

Permalink
Fix httpbp tests now that all middleware is before fault injection.
Browse files Browse the repository at this point in the history
  • Loading branch information
HiramSilvey committed Jan 17, 2025
1 parent 3ee3538 commit 0facb59
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
1 change: 1 addition & 0 deletions httpbp/client_middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ func (c clientFaultMiddleware) Middleware() ClientMiddleware {
"Content-Type": {"text/plain; charset=utf-8"},
"X-Content-Type-Options": {"nosniff"},
},
Body: http.NoBody,
ContentLength: 0,
TransferEncoding: req.TransferEncoding,
Request: req,
Expand Down
56 changes: 31 additions & 25 deletions httpbp/client_middlewares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,11 @@ func TestFaultInjection(t *testing.T) {
faultAbortMessageHeader string
faultAbortPercentageHeader string

wantResp *http.Response
wantStatusCode int
}{
{
name: "no fault specified",
wantResp: &http.Response{
StatusCode: http.StatusOK,
},
name: "no fault specified",
wantStatusCode: http.StatusOK,
},
{
name: "abort",
Expand All @@ -423,9 +421,7 @@ func TestFaultInjection(t *testing.T) {
faultServerMethodHeader: "testMethod",
faultAbortCodeHeader: "500",

wantResp: &http.Response{
StatusCode: http.StatusInternalServerError,
},
wantStatusCode: http.StatusInternalServerError,
},
{
name: "service does not match",
Expand All @@ -434,9 +430,7 @@ func TestFaultInjection(t *testing.T) {
faultServerMethodHeader: "testMethod",
faultAbortCodeHeader: "500",

wantResp: &http.Response{
StatusCode: http.StatusOK,
},
wantStatusCode: http.StatusOK,
},
{
name: "method does not match",
Expand All @@ -445,9 +439,7 @@ func TestFaultInjection(t *testing.T) {
faultServerMethodHeader: "fooMethod",
faultAbortCodeHeader: "500",

wantResp: &http.Response{
StatusCode: http.StatusOK,
},
wantStatusCode: http.StatusOK,
},
{
name: "less than min abort code",
Expand All @@ -456,9 +448,7 @@ func TestFaultInjection(t *testing.T) {
faultServerMethodHeader: "testMethod",
faultAbortCodeHeader: "99",

wantResp: &http.Response{
StatusCode: http.StatusOK,
},
wantStatusCode: http.StatusOK,
},
{
name: "greater than max abort code",
Expand All @@ -467,9 +457,7 @@ func TestFaultInjection(t *testing.T) {
faultServerMethodHeader: "testMethod",
faultAbortCodeHeader: "600",

wantResp: &http.Response{
StatusCode: http.StatusOK,
},
wantStatusCode: http.StatusOK,
},
}

Expand Down Expand Up @@ -524,11 +512,29 @@ func TestFaultInjection(t *testing.T) {

resp, err := client.Do(req)

if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if tt.wantResp.StatusCode != resp.StatusCode {
t.Fatalf("expected response code %v, got %v", tt.wantResp.StatusCode, resp.StatusCode)
if tt.wantStatusCode < 400 {
if tt.wantStatusCode != resp.StatusCode {
t.Fatalf("expected response code %d, got %d", tt.wantStatusCode, resp.StatusCode)
}
} else {
if err == nil {
t.Fatal("expected err, got nil")
}

var gotErrCode int

var unwrapped interface{ Unwrap() []error }
if errors.As(err.(*url.Error).Err, &unwrapped) {
errs := unwrapped.Unwrap()
if len(errs) == 0 {
t.Fatal("expected at least 1 err, got 0")
}
gotErrCode = errs[0].(*ClientError).StatusCode
}

if tt.wantStatusCode != gotErrCode {
t.Fatalf("expected error code %d, got %d", tt.wantStatusCode, gotErrCode)
}
}
})
}
Expand Down

0 comments on commit 0facb59

Please sign in to comment.