This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
service_test.go
125 lines (112 loc) · 2.86 KB
/
service_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package command
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
httptransport "github.com/go-kit/kit/transport/http"
"github.com/micromdm/command/service/mock"
"github.com/micromdm/mdm"
)
func TestNewCommandHTTP(t *testing.T) {
client := setup(t)
defer client.Close()
request := mustMarshalJSONRequest(t, &mdm.CommandRequest{
RequestType: "SomeMDMCommand",
UDID: "some-device",
})
var httpTests = []struct {
name string
method mock.NewCommandFunc
request io.Reader
expectStatus int
}{
{
name: "happy_path",
method: mock.ReturnMockPayload,
request: request,
expectStatus: http.StatusCreated,
},
{
name: "bad_request",
method: mock.ReturnMockPayload,
request: mustMarshalJSONRequest(t, new(mdm.CommandRequest)),
expectStatus: http.StatusBadRequest,
},
{
name: "limit_reader",
method: mock.ReturnMockPayload,
request: neverEnding('a'),
expectStatus: http.StatusBadRequest,
},
}
for _, tt := range httpTests {
t.Run(tt.name, func(t *testing.T) {
client.svc.NewCommandFunc = tt.method
resp := client.Do(t, "POST", tt.request)
if want, have := tt.expectStatus, resp.StatusCode; want != have {
t.Fatalf("want %d, have %d", want, have)
}
var p struct {
Payload *mdm.Payload
Err string
}
if err := json.NewDecoder(resp.Body).Decode(&p); err != nil {
t.Fatalf("failed to decode json request")
}
if !client.svc.NewCommandInvoked &&
resp.StatusCode == http.StatusCreated {
t.Errorf("request suceeded without invoking service method.")
}
})
}
}
// a never ending io.Reader for testing that the server terminates a request
// with a too large body.
type neverEnding byte
func (b neverEnding) Read(p []byte) (n int, err error) {
for i := range p {
p[i] = byte(b)
}
return len(p), nil
}
func mustMarshalJSONRequest(t *testing.T, req interface{}) *bytes.Buffer {
buf := new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(req)
if err != nil {
t.Fatalf("failed to marshal request: %v", err)
}
return buf
}
type client struct {
*httptest.Server
svc *mock.CommandService
client *http.Client
}
func (s client) Do(t *testing.T, method string, body io.Reader) *http.Response {
req, err := http.NewRequest(method, s.URL, body)
if err != nil {
t.Fatalf("failed to create http request, err = %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
t.Fatalf("http request failed: err = %v", err)
}
return resp
}
func setup(t *testing.T) client {
svc := &mock.CommandService{}
e := Endpoints{
NewCommandEndpoint: MakeNewCommandEndpoint(svc),
}
h := MakeHTTPHandlers(
context.Background(),
e,
httptransport.ServerErrorEncoder(EncodeError),
)
s := httptest.NewServer(h.NewCommandHandler)
return client{s, svc, http.DefaultClient}
}