-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patherrors_test.go
More file actions
42 lines (36 loc) · 896 Bytes
/
errors_test.go
File metadata and controls
42 lines (36 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package httpx
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/imroc/req/v3"
)
func TestHTTPErrorString(t *testing.T) {
err := (&HTTPError{StatusCode: 400, Status: "Bad Request"}).Error()
if err == "" {
t.Fatalf("expected error string")
}
}
func TestNewHTTPErrorNilResponse(t *testing.T) {
err := newHTTPError(nil)
if err.StatusCode != 0 {
t.Fatalf("expected status 0")
}
if err.Status == "" {
t.Fatalf("expected status message")
}
}
func TestNewHTTPErrorResponse(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
t.Cleanup(srv.Close)
resp, reqErr := req.C().R().Get(srv.URL)
if reqErr != nil {
t.Fatalf("request failed: %v", reqErr)
}
err := newHTTPError(resp)
if err.StatusCode != 500 {
t.Fatalf("status code = %d", err.StatusCode)
}
}