From 0facb59780102c0d7ed2e07004ba8ff13883b0e9 Mon Sep 17 00:00:00 2001 From: Hiram Silvey <9485196+HiramSilvey@users.noreply.github.com> Date: Fri, 17 Jan 2025 15:28:02 -0500 Subject: [PATCH] Fix httpbp tests now that all middleware is before fault injection. --- httpbp/client_middlewares.go | 1 + httpbp/client_middlewares_test.go | 56 +++++++++++++++++-------------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/httpbp/client_middlewares.go b/httpbp/client_middlewares.go index 7a1ba5b02..fd13b2edf 100644 --- a/httpbp/client_middlewares.go +++ b/httpbp/client_middlewares.go @@ -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, diff --git a/httpbp/client_middlewares_test.go b/httpbp/client_middlewares_test.go index f0c213806..c7332006d 100644 --- a/httpbp/client_middlewares_test.go +++ b/httpbp/client_middlewares_test.go @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -467,9 +457,7 @@ func TestFaultInjection(t *testing.T) { faultServerMethodHeader: "testMethod", faultAbortCodeHeader: "600", - wantResp: &http.Response{ - StatusCode: http.StatusOK, - }, + wantStatusCode: http.StatusOK, }, } @@ -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) + } } }) }