-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from Antonboom/feat/require-http-handlers
go-require: require-error: support HTTP handlers
- Loading branch information
Showing
17 changed files
with
492 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
analyzer/testdata/src/go-require-http-handlers/go_require_http_handlers_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package gorequireissue66issue73_test | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
// NOTE(a.telyshev): Neither `assert` nor `require` is the best way to test an HTTP handler: | ||
// it leads to redundant stack traces (up to runtime assembler), as well as undefined behaviour (in `require` case). | ||
// Use HTTP mechanisms (status code, headers, response data) and place assertions in the main test function. | ||
|
||
func TestServer_Assert(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
file, err := os.Open("some file.json") | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
data, err := io.ReadAll(file) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
_, err = w.Write(data) | ||
assert.NoError(t, err) | ||
})) | ||
defer ts.Close() | ||
|
||
client := ts.Client() | ||
|
||
req, err := http.NewRequest("GET", ts.URL+"/assert", nil) | ||
assert.NoError(t, err) // want "require-error: for error assertions use require" | ||
|
||
resp, err := client.Do(req) | ||
require.NoError(t, err) | ||
defer func() { | ||
assert.NoError(t, resp.Body.Close()) | ||
}() | ||
|
||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
} | ||
|
||
func TestServer_Require(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
file, err := os.Open("some file.json") | ||
require.NoError(t, err) // want "go-require: do not use require in http handlers" | ||
|
||
data, err := io.ReadAll(file) | ||
require.NoError(t, err) // want "go-require: do not use require in http handlers" | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
_, err = w.Write(data) | ||
if !assert.NoError(t, err) { | ||
assert.FailNow(t, err.Error()) // want "go-require: do not use assert\\.FailNow in http handlers" | ||
} | ||
})) | ||
defer ts.Close() | ||
|
||
client := ts.Client() | ||
client.Timeout = 10 * time.Second | ||
|
||
req, err := http.NewRequest("GET", ts.URL+"/require", nil) | ||
require.NoError(t, err) | ||
|
||
resp, err := client.Do(req) | ||
require.NoError(t, err) | ||
defer func() { | ||
require.NoError(t, resp.Body.Close()) | ||
}() | ||
|
||
require.Equal(t, http.StatusOK, resp.StatusCode) | ||
} | ||
|
||
type ServerSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func TestServerSuite(t *testing.T) { | ||
suite.Run(t, &ServerSuite{}) | ||
} | ||
|
||
func (s *ServerSuite) TestServer() { | ||
httptest.NewServer(http.HandlerFunc(s.handler)) | ||
httptest.NewServer(s) | ||
} | ||
|
||
func (s *ServerSuite) ServeHTTP(w http.ResponseWriter, _ *http.Request) { | ||
s.T().Helper() | ||
|
||
file, err := os.Open("some file.json") | ||
s.Require().NoError(err) // want "go-require: do not use require in http handlers" | ||
|
||
data, err := io.ReadAll(file) | ||
if !s.NoError(err) { | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
_, err = w.Write(data) | ||
if !s.NoError(err) { | ||
s.FailNow(err.Error()) // want "go-require: do not use s\\.FailNow in http handlers" | ||
} | ||
} | ||
|
||
func (s *ServerSuite) handler(w http.ResponseWriter, _ *http.Request) { | ||
s.T().Helper() | ||
|
||
file, err := os.Open("some file.json") | ||
s.Require().NoError(err) // want "go-require: do not use require in http handlers" | ||
|
||
data, err := io.ReadAll(file) | ||
if !s.NoError(err) { | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
_, err = w.Write(data) | ||
if !s.NoError(err) { | ||
s.FailNow(err.Error()) // want "go-require: do not use s\\.FailNow in http handlers" | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...yzer/testdata/src/go-require-ignore-http-handlers/go_require_ignore_http_handlers_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package gorequireignorehttphandlers_test | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
func TestServer_Require(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
file, err := os.Open("some file.json") | ||
require.NoError(t, err) | ||
|
||
data, err := io.ReadAll(file) | ||
require.NoError(t, err) | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
_, err = w.Write(data) | ||
if !assert.NoError(t, err) { | ||
assert.FailNow(t, err.Error()) | ||
} | ||
})) | ||
defer ts.Close() | ||
|
||
client := ts.Client() | ||
client.Timeout = 10 * time.Second | ||
|
||
req, err := http.NewRequest("GET", ts.URL+"/require", nil) | ||
require.NoError(t, err) | ||
|
||
statusCode := make(chan int) | ||
go func() { | ||
resp, err := client.Do(req) | ||
require.NoError(t, err) // want "go-require: require must only be used in the goroutine running the test function" | ||
defer func() { | ||
require.NoError(t, resp.Body.Close()) // want "go-require: require must only be used in the goroutine running the test function" | ||
}() | ||
statusCode <- resp.StatusCode | ||
}() | ||
|
||
require.Equal(t, http.StatusOK, <-statusCode) | ||
} | ||
|
||
type SomeServerSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func TestSomeServerSuite(t *testing.T) { | ||
suite.Run(t, &SomeServerSuite{}) | ||
} | ||
|
||
func (s *SomeServerSuite) TestServer() { | ||
httptest.NewServer(http.HandlerFunc(s.handler)) | ||
httptest.NewServer(s) | ||
} | ||
|
||
func (s *SomeServerSuite) ServeHTTP(hres http.ResponseWriter, hreq *http.Request) { | ||
var req MyRequest | ||
err := json.NewDecoder(hreq.Body).Decode(&req) | ||
s.Require().NoError(err) | ||
s.Equal("42", req.ID) | ||
} | ||
|
||
func (s *SomeServerSuite) handler(hres http.ResponseWriter, hreq *http.Request) { | ||
var req MyRequest | ||
err := json.NewDecoder(hreq.Body).Decode(&req) | ||
s.Require().NoError(err) | ||
s.Equal("42", req.ID) | ||
} | ||
|
||
type MyRequest struct { | ||
ID string | ||
} |
50 changes: 0 additions & 50 deletions
50
analyzer/testdata/src/go-require-issue66/gorequire_requireerror_conflict_test.go
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.