forked from colinjfw/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go
91 lines (78 loc) · 1.88 KB
/
http_test.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package rules
import (
"bytes"
"io"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/require"
)
type mockHTTPClient struct {
err error
resp func(string) *http.Response
}
func (mockHTTPClient) SetTimeout(time.Duration) {}
func (c mockHTTPClient) Get(url string) (*http.Response, error) {
if c.err != nil {
return nil, c.err
}
return c.resp(url), nil
}
func (c mockHTTPClient) Post(url, contentType string, body io.Reader) (*http.Response, error) {
if c.err != nil {
return nil, c.err
}
return c.resp(url), nil
}
type readCloser struct {
*bytes.Buffer
}
func (readCloser) Close() error {
return nil
}
func singleEndpointMockClient(t *testing.T, url, bodyJSON string, statusCode int) func(time.Duration) httpClient {
body := readCloser{Buffer: &bytes.Buffer{}}
body.WriteString(bodyJSON)
return func(time.Duration) httpClient {
return mockHTTPClient{
resp: func(reqURL string) *http.Response {
if reqURL != url {
require.Fail(t, "invalid url")
}
return &http.Response{
Body: body,
StatusCode: statusCode,
}
},
}
}
}
func TestIsValidURL(t *testing.T) {
tests := []struct {
URL string
Expected bool
}{
{URL: "", Expected: false},
{URL: "aksdjflaskjd", Expected: false},
{URL: "http://127.0.0.1:8001", Expected: true},
{URL: "https://snake.battlesnake.io/something/something", Expected: true},
}
for _, test := range tests {
actual := isValidURL(test.URL)
require.Equal(t, test.Expected, actual, "URL: %s", test.URL)
}
}
func TestGetURL(t *testing.T) {
tests := []struct {
URL string
Path string
Expected string
}{
{URL: "http://localhost", Path: "move", Expected: "http://localhost/move"},
{URL: "http://localhost/", Path: "move", Expected: "http://localhost/move"},
}
for _, test := range tests {
actual := getURL(test.URL, test.Path)
require.Equal(t, test.Expected, actual)
}
}