Skip to content

Commit

Permalink
🐛 [Bug]: Adaptator + otelfiber issue #2641 (#2772)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneWerner87 committed Dec 22, 2023
1 parent 1fac52a commit c49faf9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions middleware/adaptor/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func HTTPMiddleware(mw func(http.Handler) http.Handler) fiber.Handler {
c.Request().Header.SetMethod(r.Method)
c.Request().SetRequestURI(r.RequestURI)
c.Request().SetHost(r.Host)
c.Request().Header.SetHost(r.Host)
for key, val := range r.Header {
for _, v := range val {
c.Request().Header.Set(key, v)
Expand Down Expand Up @@ -128,6 +129,7 @@ func handlerFunc(app *fiber.App, h ...fiber.Handler) http.HandlerFunc {
req.Header.SetMethod(r.Method)
req.SetRequestURI(r.RequestURI)
req.SetHost(r.Host)
req.Header.SetHost(r.Host)
for key, val := range r.Header {
for _, v := range val {
req.Header.Set(key, v)
Expand Down
6 changes: 6 additions & 0 deletions middleware/adaptor/adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ var (
)

func Test_HTTPMiddleware(t *testing.T) {
const expectedHost = "foobar.com"
tests := []struct {
name string
url string
Expand Down Expand Up @@ -148,6 +149,7 @@ func Test_HTTPMiddleware(t *testing.T) {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

r = r.WithContext(context.WithValue(r.Context(), TestContextKey, "okay"))
r = r.WithContext(context.WithValue(r.Context(), TestContextSecondKey, "not_okay"))
r = r.WithContext(context.WithValue(r.Context(), TestContextSecondKey, "okay"))
Expand Down Expand Up @@ -180,6 +182,7 @@ func Test_HTTPMiddleware(t *testing.T) {

for _, tt := range tests {
req, err := http.NewRequestWithContext(context.Background(), tt.method, tt.url, nil)
req.Host = expectedHost
utils.AssertEqual(t, nil, err)

resp, err := app.Test(req)
Expand All @@ -188,6 +191,7 @@ func Test_HTTPMiddleware(t *testing.T) {
}

req, err := http.NewRequestWithContext(context.Background(), fiber.MethodPost, "/", nil)
req.Host = expectedHost
utils.AssertEqual(t, nil, err)

resp, err := app.Test(req)
Expand Down Expand Up @@ -239,6 +243,8 @@ func testFiberToHandlerFunc(t *testing.T, checkDefaultPort bool, app ...*fiber.A
utils.AssertEqual(t, expectedRequestURI, string(c.Context().RequestURI()), "RequestURI")
utils.AssertEqual(t, expectedContentLength, c.Context().Request.Header.ContentLength(), "ContentLength")
utils.AssertEqual(t, expectedHost, c.Hostname(), "Host")
utils.AssertEqual(t, expectedHost, string(c.Request().Header.Host()), "Host")
utils.AssertEqual(t, "http://"+expectedHost, c.BaseURL(), "BaseURL")
utils.AssertEqual(t, expectedRemoteAddr, c.Context().RemoteAddr().String(), "RemoteAddr")

body := string(c.Body())
Expand Down

0 comments on commit c49faf9

Please sign in to comment.