-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
98 lines (80 loc) · 2.91 KB
/
main_test.go
File metadata and controls
98 lines (80 loc) · 2.91 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
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
package main
import (
"net/http"
"testing"
"net/http/httptest"
"github.com/stretchr/testify/assert"
"io/ioutil"
"KeyVal/router"
"KeyVal/model"
"bytes"
"encoding/json"
"strings"
)
func testPOST(t *testing.T, url string, body []byte, respStatus int, rspBody []byte){
resp, err := http.Post(url,"application/json", bytes.NewBuffer(body))
defer resp.Body.Close()
assert.NoError(t, err)
body, ioerr := ioutil.ReadAll(resp.Body)
assert.NoError(t, ioerr)
assert.Equal(t, strings.TrimSpace(string(rspBody)),strings.TrimSpace(string(body)), "resp body should match")
assert.Equal(t,respStatus, resp.StatusCode, "StatusCodes did not match")
}
func testGET(t *testing.T, url string, respStatus int, rspBody []byte){
resp, err := http.Get(url)
defer resp.Body.Close()
assert.NoError(t, err)
body, ioerr := ioutil.ReadAll(resp.Body)
assert.NoError(t, ioerr)
assert.Equal(t, strings.TrimSpace(string(rspBody)), strings.TrimSpace(string(body)), "resp body should match")
assert.Equal(t,respStatus, resp.StatusCode, "StatusCodes did not match")
}
func testPUT(t *testing.T, url string, body []byte, respStatus int, rspBody []byte){
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(body))
if err != nil {
t.Error(err)
}
req.Header.Set("Content-Type", "application/json")
resp,err := http.DefaultClient.Do(req)
defer resp.Body.Close()
assert.NoError(t, err)
body, ioerr := ioutil.ReadAll(resp.Body)
assert.NoError(t, ioerr)
assert.Equal(t, strings.TrimSpace(string(rspBody)), strings.TrimSpace(string(body)), "resp body should match")
assert.Equal(t,respStatus, resp.StatusCode, "StatusCodes did not match")
}
func testDELETE(t *testing.T, url string, respStatus int, rspBody string){
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
t.Error(err)
}
req.Header.Set("Content-Type", "application/json")
resp,err := http.DefaultClient.Do(req)
defer resp.Body.Close()
assert.NoError(t, err)
body, ioerr := ioutil.ReadAll(resp.Body)
assert.NoError(t, ioerr)
assert.Equal(t, strings.TrimSpace(string(rspBody)), strings.TrimSpace(string(body)), "resp body should match")
assert.Equal(t,respStatus, resp.StatusCode, "StatusCodes did not match")
}
func TestHttpServer(t *testing.T) {
dbName := "default.db"
//Setup backend db
model.ResetDB(dbName)
defer model.CleanDB(dbName)
router.KeyValDAL = model.OpenDB(dbName)
//Start test server
r := router.CreateServiceHandlers()
ts := httptest.NewServer(r)
defer ts.Close()
jsonData := &model.KeyValData{"hello","world"}
jsonStr,_ := json.Marshal(jsonData)
testPOST(t,ts.URL+"/key",jsonStr,200,jsonStr)
testGET(t,ts.URL+"/key/hello",200,jsonStr)
jsonData = &model.KeyValData{"hello","world2"}
jsonStr,_ = json.Marshal(jsonData)
testPUT(t,ts.URL+"/key/hello",jsonStr,200,jsonStr)
testGET(t,ts.URL+"/key/hello",200,jsonStr)
testDELETE(t,ts.URL+"/key/hello",200,"true")
testGET(t,ts.URL+"/key/hello",500, []byte("{\"error\":\"sql: no rows in result set\"}"))
}