-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_test.go
75 lines (63 loc) · 1.65 KB
/
route_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
package switchboard_test
import (
"io"
"io/ioutil"
"net/http/httptest"
"strings"
"testing"
"github.com/vanstee/switchboard"
)
func TestExecuteSimpleCommand(t *testing.T) {
route := &switchboard.BasicRoute{
Path: "/users",
Command: &switchboard.Command{
Driver: &FakeDriver{
Stdout: `HTTP_CONTENT_TYPE: application/json
HTTP_STATUS_CODE: 201
{ "user": { "id": 1, "name": "Jimmy" } }`,
},
},
Methods: []string{"POST"},
}
req := httptest.NewRequest(
"POST",
"http://example.com/users",
strings.NewReader(`{ "user": { "name": "Jimmy" } }`),
)
w := httptest.NewRecorder()
pipeline := switchboard.Pipeline{route}
pipeline.Handle(w, req)
resp := w.Result()
if resp.StatusCode != 201 {
t.Errorf("excepted response status to be %d, got %d", 201, resp.StatusCode)
}
contentType := resp.Header.Get("Content-Type")
if contentType != "application/json" {
t.Errorf("excepted Content-Type header to equal %s, got %s", "application/json", contentType)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("ioutil.ReadAll returned an error: %s", err)
}
if string(body) != "{ \"user\": { \"id\": 1, \"name\": \"Jimmy\" } }\n" {
t.Errorf("expected response body was incorrect")
}
}
type FakeDriver struct {
Stdout string
Stderr string
Status int64
Err error
}
func (driver FakeDriver) Execute(command *switchboard.Command, env []string, streams *switchboard.Streams) (int64, error) {
var err error
_, err = io.WriteString(streams.Stdout, driver.Stdout)
if err != nil {
return -1, err
}
_, err = io.WriteString(streams.Stderr, driver.Stderr)
if err != nil {
return -1, err
}
return driver.Status, driver.Err
}