Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
go-version: [ "1.23", "1.24" ]
runs-on: ubuntu-latest
env:
GOLANGCI_LINT_VERSION: v2.0.2
GOLANGCI_LINT_VERSION: v2.1.5

steps:
- name: Checkout code
Expand Down
8 changes: 6 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
version: "2"
run:
tests: false

formatters:
enable:
Expand All @@ -27,9 +25,12 @@ linters:
- err113
- exhaustive
- exhaustruct
- funcorder
- funlen
- gochecknoglobals
- mnd
- nlreturn
- paralleltest
- varnamelen
- wrapcheck
- wsl
Expand All @@ -47,3 +48,6 @@ linters:
- linters:
- containedctx
path: retry/retry.go
- linters:
- asasalint
path: _test.go
180 changes: 151 additions & 29 deletions http/server_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package http_test

import (
"bytes"
"io/ioutil"
"context"
"io"
"net/http"
"testing"

Expand All @@ -17,11 +17,19 @@ func TestServer_HandlesExpectation(t *testing.T) {

s.On(http.MethodGet, "/test/path")

res, err := http.Get(s.URL() + "/test/path")
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.URL()+"/test/path", nil)
require.NoError(t, err)
res, err := http.DefaultClient.Do(req)

require.NoError(t, err)
assert.Equal(t, 200, res.StatusCode)

s.AssertExpectations()

_ = res.Body.Close()
}

func TestServer_HandlesExpectationWithQuery(t *testing.T) {
Expand All @@ -30,10 +38,18 @@ func TestServer_HandlesExpectationWithQuery(t *testing.T) {

s.On(http.MethodGet, "/test/path?p=some%2Fpath")

res, err := http.Get(s.URL() + "/test/path?p=some%2Fpath")
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.URL()+"/test/path?p=some%2Fpath", nil)
require.NoError(t, err)
res, err := http.DefaultClient.Do(req)

require.NoError(t, err)
assert.Equal(t, 200, res.StatusCode)
s.AssertExpectations()

_ = res.Body.Close()
}

func TestServer_HandlesAnythingMethodExpectation(t *testing.T) {
Expand All @@ -42,10 +58,18 @@ func TestServer_HandlesAnythingMethodExpectation(t *testing.T) {

s.On(httptest.Anything, "/test/path")

res, err := http.Post(s.URL()+"/test/path", "text/plain", bytes.NewReader([]byte{}))
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

req, err := http.NewRequestWithContext(ctx, http.MethodPost, s.URL()+"/test/path", nil)
require.NoError(t, err)
res, err := http.DefaultClient.Do(req)

require.NoError(t, err)
assert.Equal(t, 200, res.StatusCode)
s.AssertExpectations()

_ = res.Body.Close()
}

func TestServer_HandlesAnythingPathExpectation(t *testing.T) {
Expand All @@ -54,10 +78,18 @@ func TestServer_HandlesAnythingPathExpectation(t *testing.T) {

s.On(http.MethodGet, httptest.Anything)

res, err := http.Get(s.URL() + "/test/path")
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.URL()+"/test/path", nil)
require.NoError(t, err)
res, err := http.DefaultClient.Do(req)

require.NoError(t, err)
assert.Equal(t, 200, res.StatusCode)
s.AssertExpectations()

_ = res.Body.Close()
}

func TestServer_HandlesWildcardPathExpectation(t *testing.T) {
Expand All @@ -66,10 +98,18 @@ func TestServer_HandlesWildcardPathExpectation(t *testing.T) {

s.On(http.MethodGet, "/test/*")

res, err := http.Get(s.URL() + "/test/path")
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.URL()+"/test/path", nil)
require.NoError(t, err)
res, err := http.DefaultClient.Do(req)

require.NoError(t, err)
assert.Equal(t, 200, res.StatusCode)
s.AssertExpectations()

_ = res.Body.Close()
}

func TestServer_HandlesUnexpectedMethodRequest(t *testing.T) {
Expand All @@ -85,7 +125,15 @@ func TestServer_HandlesUnexpectedMethodRequest(t *testing.T) {

s.On("POST", "/")

_, _ = http.Get(s.URL() + "/test/path")
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.URL()+"/test/path", nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)

require.NoError(t, err)
_ = resp.Body.Close()
}

func TestServer_HandlesUnexpectedPathRequest(t *testing.T) {
Expand All @@ -102,7 +150,14 @@ func TestServer_HandlesUnexpectedPathRequest(t *testing.T) {

s.On(http.MethodGet, "/")

_, _ = http.Get(s.URL() + "/test/path")
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.URL()+"/test/path", nil)
require.NoError(t, err)
resp, _ := http.DefaultClient.Do(req)

_ = resp.Body.Close()
}

func TestServer_HandlesUnexpectedPathQueryRequest(t *testing.T) {
Expand All @@ -118,7 +173,14 @@ func TestServer_HandlesUnexpectedPathQueryRequest(t *testing.T) {
s.On(http.MethodGet, "/test/path?a=other")
s.On(http.MethodGet, "/test/path?p=something")

_, _ = http.Get(s.URL() + "/test/path?p=somethingelse")
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.URL()+"/test/path?p=somethingelse", nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
_ = resp.Body.Close()
}

func TestServer_HandlesExpectationNTimes(t *testing.T) {
Expand All @@ -133,9 +195,20 @@ func TestServer_HandlesExpectationNTimes(t *testing.T) {
t.Cleanup(s.Close)
s.On(http.MethodGet, "/test/path").Times(2)

_, _ = http.Get(s.URL() + "/test/path")
_, _ = http.Get(s.URL() + "/test/path")
_, _ = http.Get(s.URL() + "/test/path")
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.URL()+"/test/path", nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
_ = resp.Body.Close()
resp, err = http.DefaultClient.Do(req)
require.NoError(t, err)
_ = resp.Body.Close()
resp, err = http.DefaultClient.Do(req)
require.NoError(t, err)
_ = resp.Body.Close()

s.AssertExpectations()
}
Expand All @@ -152,8 +225,17 @@ func TestServer_HandlesExpectationUnlimitedTimes(t *testing.T) {
t.Cleanup(s.Close)
s.On(http.MethodGet, "/test/path")

_, _ = http.Get(s.URL() + "/test/path")
_, _ = http.Get(s.URL() + "/test/path")
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.URL()+"/test/path", nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
_ = resp.Body.Close()
resp, err = http.DefaultClient.Do(req)
require.NoError(t, err)
_ = resp.Body.Close()

s.AssertExpectations()
}
Expand All @@ -164,10 +246,16 @@ func TestServer_ExpectationReturnsBodyBytes(t *testing.T) {

s.On(http.MethodGet, "/test/path").Returns(400, []byte("test"))

res, err := http.Get(s.URL() + "/test/path")
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.URL()+"/test/path", nil)
require.NoError(t, err)
res, err := http.DefaultClient.Do(req)

require.NoError(t, err)
assert.Equal(t, 400, res.StatusCode)
b, _ := ioutil.ReadAll(res.Body)
b, _ := io.ReadAll(res.Body)
assert.Equal(t, []byte("test"), b)

_ = res.Body.Close()
Expand All @@ -179,10 +267,16 @@ func TestServer_ExpectationReturnsBodyString(t *testing.T) {

s.On(http.MethodGet, "/test/path").ReturnsString(400, "test")

res, err := http.Get(s.URL() + "/test/path")
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.URL()+"/test/path", nil)
require.NoError(t, err)
res, err := http.DefaultClient.Do(req)

require.NoError(t, err)
assert.Equal(t, 400, res.StatusCode)
b, _ := ioutil.ReadAll(res.Body)
b, _ := io.ReadAll(res.Body)
assert.Equal(t, []byte("test"), b)

_ = res.Body.Close()
Expand All @@ -194,11 +288,17 @@ func TestServer_ExpectationReturnsStatusCode(t *testing.T) {

s.On(http.MethodGet, "/test/path").ReturnsStatus(400)

res, err := http.Get(s.URL() + "/test/path")
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.URL()+"/test/path", nil)
require.NoError(t, err)
res, err := http.DefaultClient.Do(req)

require.NoError(t, err)
assert.Equal(t, 400, res.StatusCode)
b, _ := ioutil.ReadAll(res.Body)
assert.Len(t, b, 0)
b, _ := io.ReadAll(res.Body)
assert.Empty(t, b)

_ = res.Body.Close()
}
Expand All @@ -209,9 +309,15 @@ func TestServer_ExpectationReturnsHeaders(t *testing.T) {

s.On(http.MethodGet, "/test/path").Header("foo", "bar").ReturnsStatus(200)

res, err := http.Get(s.URL() + "/test/path")
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.URL()+"/test/path", nil)
require.NoError(t, err)
v := res.Header.Get("foo")
res, err := http.DefaultClient.Do(req)

require.NoError(t, err)
v := res.Header.Get("Foo")
assert.Equal(t, "bar", v)

_ = res.Body.Close()
Expand All @@ -221,13 +327,21 @@ func TestServer_ExpectationUsesHandleFunc(t *testing.T) {
s := httptest.NewServer(t)
t.Cleanup(s.Close)

s.On(http.MethodGet, "/test/path").Handle(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400)
s.On(http.MethodGet, "/test/path").Handle(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusBadRequest)
})

res, err := http.Get(s.URL() + "/test/path")
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.URL()+"/test/path", nil)
require.NoError(t, err)
res, err := http.DefaultClient.Do(req)

require.NoError(t, err)
assert.Equal(t, 400, res.StatusCode)

_ = res.Body.Close()
}

func TestServer_AssertExpectations(t *testing.T) {
Expand All @@ -242,10 +356,18 @@ func TestServer_AssertExpectations(t *testing.T) {
t.Cleanup(s.Close)
s.On(http.MethodGet, "/").Times(1)

_, err := http.Get(s.URL() + "/")
assert.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.URL()+"/", nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)

require.NoError(t, err)

s.AssertExpectations()

_ = resp.Body.Close()
}

func TestServer_AssertExpectationsOnUnlimited(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion retry/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestRunWith_AllowsPassing(t *testing.T) {
start := time.Now()
go func() {
defer wg.Done()
retry.RunWith(mockT, retry.NewCounter(3, 10*time.Millisecond), func(t *retry.SubT) {
retry.RunWith(mockT, retry.NewCounter(3, 10*time.Millisecond), func(*retry.SubT) {
runs++
})
}()
Expand Down
Loading