-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
72 lines (57 loc) · 1.83 KB
/
main_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
package delivery
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func executeRequest(req *http.Request) *httptest.ResponseRecorder {
rr := httptest.NewRecorder()
r := Router()
r.ServeHTTP(rr, req)
return rr
}
func checkResponseCode(t *testing.T, expected, actual int) {
if expected != actual {
t.Errorf("Expected response code %d. Got %d\n", expected, actual)
}
}
func TestCreateDeliveryOrder(t *testing.T) {
reqBody := []byte(`{
"api_key": "KTLgep79E5EnUjOmoHOyxuHBV21bZuxoCg5iHw9CfMqdo79dTjLTYZCRVHAu",
"pickup_address": "Vita Towers, Kofo Abayomi Street, Lagos, Nigeria",
"pickup_latitude": "6.435275500000001",
"pickup_longitude": "3.4147874",
"delivery_address": "21 Lugard Avenue, Lagos, Nigeria",
"delivery_latitude": "6.456150699999999",
"delivery_longitude": "3.4298536",
"pickup_name": "Foo",
"pickup_phone": "+23412345678",
"pickup_email": "example@about.com",
"delivery_name": "Bar",
"delivery_phone": "+2341234567891",
"delivery_email": "example+1@about.com",
"description": "Food delivery"
}`)
req, _ := http.NewRequest("POST", "/delivery_order", bytes.NewBuffer(reqBody))
req.Header.Set("Content-Type", "application/json")
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
var m map[string]interface{}
json.Unmarshal(response.Body.Bytes(), &m)
fmt.Println(m)
if m["order_id"] != "FFFXY-426043" {
t.Errorf("Expected order id to be 'FFFXY-426043'. Got '%v'", m["order_id"])
}
if m["distance"] != 5.0 {
t.Errorf("Expected distance to be 5. Got %v", m["distance"])
}
if m["time"] != 12.0 {
t.Errorf("Expected time to be 12. Got %v", m["time"])
}
if m["fare"] != 500.0 {
t.Errorf("Expected fare to be 500. Got %v", m["fare"])
}
}