Skip to content
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

Update middleware.go #28

Merged
merged 2 commits into from Mar 4, 2024
Merged

Update middleware.go #28

merged 2 commits into from Mar 4, 2024

Conversation

RupeshHacker
Copy link
Contributor

fixed issue #27

@coveralls
Copy link

coveralls commented Mar 2, 2024

Pull Request Test Coverage Report for Build 8136072724

Details

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.2%) to 88.077%

Totals Coverage Status
Change from base Build 8032402702: 0.2%
Covered Lines: 687
Relevant Lines: 780

💛 - Coveralls

Copy link
Member

@umputun umputun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx, makes sense. However, the change is missing a corresponding test. I'd suggest smth like this:

func TestWrap(t *testing.T) {
	handler := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
		t.Logf("%s", r.URL.String())
	})

	mw1 := func(h http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.Header().Set("X-MW1", "1")
			h.ServeHTTP(w, r)
		})
	}
	mw2 := func(h http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.Header().Set("X-MW2", "2")
			h.ServeHTTP(w, r)
		})
	}

	t.Run("no middleware", func(t *testing.T) {
		h := Wrap(handler)
		ts := httptest.NewServer(h)
		defer ts.Close()

		resp, err := http.Get(ts.URL + "/something")
		require.NoError(t, err)
		assert.Equal(t, 200, resp.StatusCode)
		assert.Equal(t, "", resp.Header.Get("X-MW1"))
		assert.Equal(t, "", resp.Header.Get("X-MW2"))
	})

	t.Run("with middleware", func(t *testing.T) {
		h := Wrap(handler, mw1, mw2)
		ts := httptest.NewServer(h)
		defer ts.Close()

		resp, err := http.Get(ts.URL + "/something")
		require.NoError(t, err)
		assert.Equal(t, 200, resp.StatusCode)
		assert.Equal(t, "1", resp.Header.Get("X-MW1"))
		assert.Equal(t, "2", resp.Header.Get("X-MW2"))
	})
}

test case updated as per issue go-pkgz#28
Copy link
Member

@umputun umputun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@umputun umputun merged commit 8e030ce into go-pkgz:master Mar 4, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants