-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
68 lines (60 loc) · 1.63 KB
/
main_test.go
File metadata and controls
68 lines (60 loc) · 1.63 KB
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
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestHelloHandler(t *testing.T) {
// Create a request to pass to our handler. The second parameter is the URL path,
// and the third parameter is the body (nil for GET requests).
reqBody := `{
"first": {
"dimensions": {
"x": 2,
"y": 2,
"z": 2
},
"center": {
"x": 0,
"y": 0,
"z": 0
}
},
"second": {
"dimensions": {
"x": 2,
"y": 2,
"z": 2
},
"center": {
"x": 0.5,
"y": 0.5,
"z": 0.5
}
}
}`
req, err := http.NewRequest("POST", "/intersect", bytes.NewBuffer([]byte(reqBody)))
if err != nil {
t.Fatal(err)
}
// Create a ResponseRecorder, which will capture the response from the handler.
rr := httptest.NewRecorder()
intersect := intersectServiceImpl{}
// Create a handler function and pass it to ServeHTTP, simulating an HTTP request.
handler := cubicHandler(intersect)
handler.ServeHTTP(rr, req)
// Check if the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
// Check the response body is what we expect.
expected := CubicResponse{}
err = json.Unmarshal([]byte(`{"success":true,"volume":3.375}`), &expected)
response := CubicResponse{}
err = json.Unmarshal(rr.Body.Bytes(), &response)
if response != expected {
t.Errorf("handler returned unexpected body: got %v want %v", response, expected)
}
}