-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplantuml_test.go
93 lines (78 loc) · 2.14 KB
/
plantuml_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
package plantuml
import (
"bufio"
"bytes"
"fmt"
"github.com/steinfletcher/apitest"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestWritesTheMeta(t *testing.T) {
recorder := aRecorder()
capture := &writer{}
NewFormatter(capture).Format(recorder)
actual := bytes.NewReader([]byte(capture.captured))
firstLine, _, err := bufio.NewReader(actual).ReadLine()
if err != nil {
panic(err)
}
if `{"host":"example.com","method":"GET","name":"some test","path":"/user"}` != string(firstLine) {
t.Fail()
}
}
func TestNewFormatter(t *testing.T) {
recorder := aRecorder()
capture := &writer{}
NewFormatter(capture).Format(recorder)
actual := capture.captured
expected, _ := ioutil.ReadFile("testdata/snapshot.txt")
if normalize(string(expected)) != normalize(actual) {
fmt.Printf("Expected '%s'\nReceived '%s'\n", string(expected), actual)
t.Fail()
}
}
type writer struct {
captured string
}
func (p *writer) Write(data []byte) (int, error) {
p.captured = strings.TrimSpace(string(data))
return -1, nil
}
func normalize(s string) string {
return strings.Join(strings.Fields(s), " ")
}
func aRecorder() *apitest.Recorder {
return apitest.NewTestRecorder().
AddTitle("title").
AddSubTitle("subTitle").
AddHttpRequest(aRequest()).
AddMessageRequest(apitest.MessageRequest{Header: "SQL Query", Body: "SELECT * FROM users", Source: "sut-a", Target: "a"}).
AddMessageResponse(apitest.MessageResponse{Header: "SQL Result", Body: "Rows count: 122", Source: "a", Target: "sut-a"}).
AddHttpResponse(aResponse()).
AddMeta(map[string]interface{}{
"path": "/user",
"name": "some test",
"host": "example.com",
"method": "GET",
})
}
func aRequest() apitest.HttpRequest {
req := httptest.NewRequest(http.MethodGet, "http://example.com/abcdef", nil)
req.Header.Set("Content-Type", "application/json")
return apitest.HttpRequest{Value: req, Source: "cli", Target: "sut-a"}
}
func aResponse() apitest.HttpResponse {
return apitest.HttpResponse{
Value: &http.Response{
StatusCode: http.StatusOK,
ProtoMajor: 1,
ProtoMinor: 1,
ContentLength: 0,
},
Source: "sut-a",
Target: "cli",
}
}