-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
91 lines (75 loc) · 2.67 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"bytes"
"encoding/json"
"fuber/location"
"fuber/models"
"fuber/user"
"net/http"
"net/http/httptest"
"testing"
)
// Simulate http request here
func executeRequest(req *http.Request) *httptest.ResponseRecorder {
rr := httptest.NewRecorder()
myRouter.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)
}
}
// Testing the APIs
func TestGetCabs(t *testing.T) {
initialize()
req, _ := http.NewRequest("GET", "/getcabs", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
var cabsList models.CabsListResponseModel
json.Unmarshal(response.Body.Bytes(), &cabsList)
// Checking to see if error exists
if cabsList.Errors != "" {
t.Errorf("Expected a list of cabs for a response. Got error")
}
}
// Testing the action of booking a cab
func TestBookCab(t *testing.T) {
initialize()
var userDetails = user.User{Pickup: location.Location{67, 100}}
payloadBytes, _ := json.Marshal(userDetails)
req, _ := http.NewRequest("POST", "/beginride", bytes.NewBuffer(payloadBytes))
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
var cab models.CabBookedResponseModel
json.Unmarshal(response.Body.Bytes(), &cab)
if cab.Errors != "" {
t.Errorf("Expected a cab. Got %s", cab.Errors)
}
}
// Testing booking and and ending a cab ride
func TestEndRide(t *testing.T) {
initialize()
var invalidUserDetails = user.User{Pickup: location.Location{67, 100}}
payloadBytes, _ := json.Marshal(invalidUserDetails)
req, _ := http.NewRequest("POST", "/endride", bytes.NewBuffer(payloadBytes))
response := executeRequest(req)
// This request should fail to status 400
checkResponseCode(t, http.StatusBadRequest, response.Code)
// Now check for valid end ride by booking a ride and then ending it
var userBookingDetails = user.User{Pickup: location.Location{67, 100}}
payloadBytes, _ = json.Marshal(userBookingDetails)
req, _ = http.NewRequest("POST", "/beginride", bytes.NewBuffer(payloadBytes))
response = executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
var validUserDetails = user.User{Pickup: location.Location{67, 100}, Drop: location.Location{70, 104}, CabId: 1, TravelTime: 6}
payloadBytes, _ = json.Marshal(validUserDetails)
req, _ = http.NewRequest("POST", "/endride", bytes.NewBuffer(payloadBytes))
response = executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
var bill models.CabRideEndResponseModel
json.Unmarshal(response.Body.Bytes(), &bill)
if bill.Errors != "" {
t.Errorf("Expected a bill. Got %s", bill.Errors)
}
}