...
1 package httperr
2
3 import (
4 "errors"
5 "net/http"
6 "net/url"
7 "reflect"
8 "testing"
9 )
10
11 func TestWithHttpRequest(t *testing.T) {
12 tests := map[string]struct {
13 request *http.Request
14 err error
15 }{
16 "simple error": {
17 request: &http.Request{
18 Method: "POST",
19 URL: &url.URL{
20 Scheme: "http",
21 Host: "localhost",
22 },
23 },
24 err: errors.New("error"),
25 },
26 }
27
28 for name, test := range tests {
29 name, test := name, test
30
31 t.Run(name, func(t *testing.T) {
32 err := WithHTTPRequest(test.err, test.request)
33
34 if got, want := err.Error(), test.err.Error(); got != want {
35 t.Errorf("error does not match the expected one\nactual: %v\nexpected: %v", got, want)
36 }
37
38 req, ok := HTTPRequest(err)
39 if !ok {
40 t.Error("error is expected to contain an HTTP request")
41 }
42
43 if got, want := req, test.request; !reflect.DeepEqual(got, want) {
44 t.Errorf("request does not match the expected one\nactual: %v\nexpected: %v", got, want)
45 }
46 })
47 }
48 }
49
View as plain text