-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_test.go
46 lines (40 loc) · 1.27 KB
/
simple_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
package examples_test
import (
"net/http"
"testing"
"github.com/sachsry/mockhttp/v1/mockhttp"
"github.com/sachsry/mockhttp/v1/response"
"github.com/stretchr/testify/assert"
)
func TestSimpleHandler(t *testing.T) {
tests := []mockhttp.TestStruct{
{
Name: "even length",
Input: mockhttp.NewRequest("GET", "/suh", ""), // This is even because the "/" counts as a char in the path
Expected: mockhttp.NewRawResponse().WithStatus(200),
},
{
Name: "odd length",
Input: mockhttp.NewRequest("GET", "/blah", ""), // This is odd because the "/" counts as a char in the path
Expected: mockhttp.NewRawResponse().WithStatus(500),
},
}
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
handleSimple(tt.Input.W, tt.Input.R)
res, err := mockhttp.ToResponse(tt.Input.Result())
assert.Nil(t, err, res)
assert.Equal(t, tt.Expected.Status(), res.Status())
})
}
}
// This handler sends a 200 if the length of the path is even, and a 500 if not.
// For example, for a request mockhttp.NewRequest("GET", "/hello", ""), "/hello" is the path
func handleSimple(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
if len(path)%2 != 0 {
response.Error(w, 500, "length of the path is odd", nil)
return
}
response.Success(w)
}