Skip to content

Commit 1910c9f

Browse files
committed
test(server/create_test): add tests for create static document and create bad document
1 parent 27a8ab0 commit 1910c9f

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ format:
1818
go fmt ./...
1919

2020
test:
21-
go test ./... -v -race
21+
go test ./... -v -race -coverprofile fmtcoverage.html fmt

internal/server/create_test.go

+31-2
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,38 @@ func (s *CreateDocumentSuite) TestCreateMultipartDocument() {
108108
require.Equal(s.T(), expectedResponse.Payload, body.Payload)
109109
}
110110

111-
func (s *CreateDocumentSuite) TestStaticCreateDocument() {}
111+
func (s *CreateDocumentSuite) TestStaticCreateDocument() {
112+
// Setup multipart/form-data body
113+
var b bytes.Buffer
114+
mw := multipart.NewWriter(&b)
115+
mw.WriteField("content", "test")
116+
mw.Close()
117+
118+
// Send request
119+
req, _ := http.NewRequest(http.MethodPost, "/", &b)
120+
req.Header.Add("Content-Type", mw.FormDataContentType())
121+
rr := executeRequest(req, s.srv)
122+
123+
// Assertions
124+
require.Equal(s.T(), http.StatusMovedPermanently, rr.Result().StatusCode)
125+
require.Equal(s.T(), "/12345678", rr.Result().Header.Get("Location"))
126+
// add a test for content-type and body?
127+
}
128+
129+
func (s *CreateDocumentSuite) TestCreateBadDocument() {
130+
req, _ := http.NewRequest(http.MethodPost, "/api/",
131+
bytes.NewReader([]byte(`{"content": "1"}`)),
132+
)
133+
req.Header.Set("Content-Type", "application/json")
134+
rr := executeRequest(req, s.srv)
112135

113-
func (s *CreateDocumentSuite) TestCreateBadDocument() {}
136+
x, _ := io.ReadAll(rr.Result().Body)
137+
var body DocumentResponse
138+
json.Unmarshal(x, &body)
139+
140+
require.Equal(s.T(), http.StatusBadRequest, rr.Result().StatusCode)
141+
require.Equal(s.T(), "Content: the length must be between 2 and 400000.", body.Error)
142+
}
114143

115144
func TestCreateDocumentSuite(t *testing.T) {
116145
suite.Run(t, new(CreateDocumentSuite))

0 commit comments

Comments
 (0)