-
Notifications
You must be signed in to change notification settings - Fork 55
/
diagram_test.go
219 lines (183 loc) · 6.02 KB
/
diagram_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package apitest
import (
"html/template"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
func TestDiagram_BadgeCSSClass(t *testing.T) {
tests := []struct {
status int
class string
}{
{status: http.StatusOK, class: "badge badge-success"},
{status: http.StatusInternalServerError, class: "badge badge-danger"},
{status: http.StatusBadRequest, class: "badge badge-warning"},
}
for _, test := range tests {
t.Run(test.class, func(t *testing.T) {
class := badgeCSSClass(test.status)
assert.Equal(t, test.class, class)
})
}
}
func TestFormatBodyContent_ShouldReplaceBody(t *testing.T) {
stream := ioutil.NopCloser(strings.NewReader("lol"))
val, err := formatBodyContent(stream, func(replacementBody io.ReadCloser) {
stream = replacementBody
})
assert.NoError(t, err)
assert.Equal(t, "lol", val)
valSecondRun, errSecondRun := formatBodyContent(stream, func(replacementBody io.ReadCloser) {
stream = replacementBody
})
assert.NoError(t, errSecondRun)
assert.Equal(t, "lol", valSecondRun)
}
func TestWebSequenceDiagram_GeneratesDSL(t *testing.T) {
wsd := webSequenceDiagramDSL{}
wsd.addRequestRow("A", "B", "request1")
wsd.addRequestRow("B", "C", "request2")
wsd.addResponseRow("C", "B", "response1")
wsd.addResponseRow("B", "A", "response2")
actual := wsd.toString()
expected := `"A"->"B": (1) request1
"B"->"C": (2) request2
"C"->>"B": (3) response1
"B"->>"A": (4) response2
`
if expected != actual {
t.Fatalf("expected=%s != \nactual=%s", expected, actual)
}
}
func TestNewSequenceDiagramFormatter_SetsDefaultPath(t *testing.T) {
formatter := SequenceDiagram()
assert.Equal(t, ".sequence", formatter.storagePath)
}
func TestNewSequenceDiagramFormatter_OverridesPath(t *testing.T) {
formatter := SequenceDiagram(".sequence-diagram")
assert.Equal(t, ".sequence-diagram", formatter.storagePath)
}
func TestRecorderBuilder(t *testing.T) {
recorder := aRecorder()
assert.Equal(t, 4, len(recorder.Events))
assert.Equal(t, "title", recorder.Title)
assert.Equal(t, "subTitle", recorder.SubTitle)
assert.Equal(t, map[string]interface{}{
"path": "/user",
"name": "some test",
"host": "example.com",
"method": "GET",
}, recorder.Meta)
assert.Equal(t, "reqSource", recorder.Events[0].(HttpRequest).Source)
assert.Equal(t, "mesReqSource", recorder.Events[1].(MessageRequest).Source)
assert.Equal(t, "mesResSource", recorder.Events[2].(MessageResponse).Source)
assert.Equal(t, "resSource", recorder.Events[3].(HttpResponse).Source)
}
func TestNewHTMLTemplateModel_ErrorsIfNoEventsDefined(t *testing.T) {
recorder := NewTestRecorder()
_, err := newHTMLTemplateModel(recorder)
assert.Equal(t, "no events are defined", err.Error())
}
func TestNewHTMLTemplateModel_Success(t *testing.T) {
recorder := aRecorder()
model, err := newHTMLTemplateModel(recorder)
assert.True(t, err == nil)
assert.Equal(t, 4, len(model.LogEntries))
assert.Equal(t, "title", model.Title)
assert.Equal(t, "subTitle", model.SubTitle)
assert.Equal(t, template.JS(`{"host":"example.com","method":"GET","name":"some test","path":"/user"}`), model.MetaJSON)
assert.Equal(t, http.StatusNoContent, model.StatusCode)
assert.Equal(t, "badge badge-success", model.BadgeClass)
assert.True(t, strings.Contains(model.WebSequenceDSL, "GET /abcdef"))
}
func aRecorder() *Recorder {
return NewTestRecorder().
AddTitle("title").
AddSubTitle("subTitle").
AddHttpRequest(aRequest()).
AddMessageRequest(MessageRequest{Header: "A", Body: "B", Source: "mesReqSource"}).
AddMessageResponse(MessageResponse{Header: "C", Body: "D", Source: "mesResSource"}).
AddHttpResponse(aResponse()).
AddMeta(map[string]interface{}{
"path": "/user",
"name": "some test",
"host": "example.com",
"method": "GET",
})
}
func TestNewHttpRequestLogEntry(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/path", strings.NewReader(`{"a": 12345}`))
logEntry, err := newHTTPRequestLogEntry(req)
assert.True(t, err == nil)
assert.True(t, strings.Contains(logEntry.Header, "GET /path"))
assert.True(t, strings.Contains(logEntry.Header, "HTTP/1.1"))
assert.JSONEq(t, logEntry.Body, `{"a": 12345}`)
}
func TestNewHttpResponseLogEntry_JSON(t *testing.T) {
response := &http.Response{
ProtoMajor: 1,
ProtoMinor: 1,
StatusCode: http.StatusOK,
ContentLength: 21,
Body: ioutil.NopCloser(strings.NewReader(`{"a": 12345}`)),
}
logEntry, err := newHTTPResponseLogEntry(response)
assert.True(t, err == nil)
assert.True(t, strings.Contains(logEntry.Header, `HTTP/1.1 200 OK`))
assert.True(t, strings.Contains(logEntry.Header, `Content-Length: 21`))
assert.JSONEq(t, logEntry.Body, `{"a": 12345}`)
}
func TestNewHttpResponseLogEntry_PlainText(t *testing.T) {
response := &http.Response{
ProtoMajor: 1,
ProtoMinor: 1,
StatusCode: http.StatusOK,
ContentLength: 21,
Body: ioutil.NopCloser(strings.NewReader(`abcdef`)),
}
logEntry, err := newHTTPResponseLogEntry(response)
assert.True(t, err == nil)
assert.True(t, strings.Contains(logEntry.Header, `HTTP/1.1 200 OK`))
assert.True(t, strings.Contains(logEntry.Header, `Content-Length: 21`))
assert.Equal(t, logEntry.Body, `abcdef`)
}
func aRequest() HttpRequest {
req := httptest.NewRequest(http.MethodGet, "http://example.com/abcdef?name=abc", nil)
req.Header.Set("Content-Type", "application/json")
return HttpRequest{Value: req, Source: "reqSource", Target: "reqTarget"}
}
func aResponse() HttpResponse {
return HttpResponse{
Value: &http.Response{
StatusCode: http.StatusNoContent,
ProtoMajor: 1,
ProtoMinor: 1,
ContentLength: 0,
},
Source: "resSource",
Target: "resTarget",
}
}
type FS struct {
CapturedCreateName string
CapturedCreateFile string
CapturedMkdirAllPath string
}
func (m *FS) create(name string) (*os.File, error) {
m.CapturedCreateName = name
file, err := ioutil.TempFile("/tmp", "apitest")
if err != nil {
panic(err)
}
m.CapturedCreateFile = file.Name()
return file, nil
}
func (m *FS) mkdirAll(path string, perm os.FileMode) error {
m.CapturedMkdirAllPath = path
return nil
}