-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutterance_test.go
63 lines (53 loc) · 1.37 KB
/
utterance_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
package main
import (
"bytes"
"mime/multipart"
"net/http"
"net/http/httptest"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/awstesting/mock"
"github.com/aws/aws-sdk-go/service/s3"
)
func TestServer(t *testing.T) {
// Create a mock AWS service and upload
server := NewServer()
server.S3 = s3.New(mock.Session, aws.NewConfig().WithRegion("us-west-2"))
w := httptest.NewRecorder()
// GET should return the form
req, err := http.NewRequest(http.MethodGet, "/", nil)
if err != nil {
t.Fatalf("failed to create GET: %s", err)
}
server.uploadHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf(
"unexpected status from GET (%d): %s",
w.Code, w.Body.String(),
)
}
// TODO test the body content
// POST should upload a file
w = httptest.NewRecorder()
var content bytes.Buffer
multi := multipart.NewWriter(&content)
fw, err := multi.CreateFormFile("file", "plain.txt")
if err != nil {
t.Fatalf("failed to create form file: %s", err)
}
fw.Write([]byte("hello"))
req, err = http.NewRequest(http.MethodPost, "/", &content)
if err != nil {
t.Fatalf("failed to create POST: %s", err)
}
req.Header.Set("Content-Type", multi.FormDataContentType())
multi.Close()
server.uploadHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf(
"unexpected status from POST (%d): %s",
w.Code, w.Body.String(),
)
}
// test the mock s3?
}