-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.test.request.go
111 lines (87 loc) · 2.84 KB
/
driver.test.request.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package driver
import (
"bytes"
"encoding/json"
"errors"
"net/http/httptest"
"net/url"
"strconv"
"sync"
"github.com/Gujarats/nearest/model/global"
"github.com/Gujarats/nearest/model/city/mock"
"github.com/Gujarats/nearest/model/driver/mock"
"github.com/Gujarats/nearest/model/city/interface"
"github.com/Gujarats/nearest/model/driver/interface"
)
// create test request that pass all parameters requirement.
func createFindDriverRequest(cityMock *cityMock.CityMock, driverMock *driverMock.DriverDataMock) (int, string, error) {
// create body params
body := url.Values{}
body.Set("latitude", "48.8588377")
body.Set("longitude", "2.2775176")
body.Set("city", "Paris")
body.Set("distance", "200")
//we pass a dummy value to pass the required params
req := httptest.NewRequest("POST", "/driver/find", bytes.NewBufferString(body.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(body.Encode())))
w := httptest.NewRecorder()
// driver mock model
var driver driverInterface.DriverInterfacce
driver = driverMock
// city mock model
var city cityInterface.CityInterfacce
city = cityMock
// craete request
handler := FindDriver(driver, city)
handler.ServeHTTP(w, req)
// check the response
resp := w.Body.Bytes()
if resp == nil {
return -1, "", errors.New("Response body is empty")
}
// response result
RespResult := global.Response{}
err := json.Unmarshal(resp, &RespResult)
if err != nil {
return -1, "", err
}
return w.Code, RespResult.Message, nil
}
// create update request with pass alll the requirement parameters
func createUpdateDriverRequest(cityMock *cityMock.CityMock, driverMock *driverMock.DriverDataMock) (int, string, error) {
// create body params
body := url.Values{}
body.Set("name", "driver1")
body.Set("id", "58ccebc7ac702fc793f9e384")
body.Set("latitude", "48.8588377")
body.Set("longitude", "2.2775176")
body.Set("status", "true")
body.Set("city", "Paris")
//we pass a dummy value to pass the required params
req := httptest.NewRequest("POST", "/driver/update", bytes.NewBufferString(body.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(body.Encode())))
w := httptest.NewRecorder()
// driver mock model
var driver driverInterface.DriverInterfacce
driver = driverMock
// city mock model
var city cityInterface.CityInterfacce
city = cityMock
m := &sync.Mutex{}
// call request to get status response
handler := UpdateDriver(m, driver, city)
handler.ServeHTTP(w, req)
// check the response
resp := w.Body.Bytes()
if resp == nil {
return -1, "", errors.New("Response body is empty")
}
RespResult := global.Response{}
err := json.Unmarshal(resp, &RespResult)
if err != nil {
return -1, "", err
}
return w.Code, RespResult.Message, nil
}