-
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.
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 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
50 changes: 50 additions & 0 deletions
50
analyzer/testdata/src/go-require-issue66/gorequire_requireerror_conflict_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,50 @@ | ||
package gorequireissue66 | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// NOTE(a.telyshev): Neither assert nor require is the best way to test an HTTP handler: | ||
// it leads to redundant stack traces, as well as EOF from the HTTP client. | ||
// Use HTTP mechanisms and place assertions in the main test function. | ||
|
||
var mockHTTPFromFile = func(t *testing.T) http.HandlerFunc { | ||
t.Helper() | ||
|
||
return func(w http.ResponseWriter, _ *http.Request) { | ||
file, err := os.Open("some file.json") | ||
assert.NoError(t, err) // want "require-error: for error assertions use require" | ||
|
||
data, err := io.ReadAll(file) | ||
require.NoError(t, err) | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
_, err = w.Write(data) | ||
assert.NoError(t, err) | ||
} | ||
} | ||
|
||
func TestGoRequireAndRequireErrorConflict(t *testing.T) { | ||
ts := httptest.NewServer(mockHTTPFromFile(t)) | ||
defer ts.Close() | ||
|
||
client := ts.Client() | ||
|
||
req, err := http.NewRequest("GET", ts.URL+"/example", nil) | ||
assert.NoError(t, err) // want "require-error: for error assertions use require" | ||
|
||
resp, err := client.Do(req) | ||
require.NoError(t, err) | ||
defer func() { | ||
require.NoError(t, resp.Body.Close()) | ||
}() | ||
|
||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
} |