-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb_test.go
More file actions
90 lines (72 loc) · 1.67 KB
/
Copy pathweb_test.go
File metadata and controls
90 lines (72 loc) · 1.67 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
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo"
"github.com/stretchr/testify/assert"
)
var (
c1 = []byte(`{
"abc": 10,
"bcd": 15,
"cde": 25,
"def": 50
}`)
c2 = []byte(`{
"a":10,
"b":20,
"c":30
}`)
)
func createOrUpdate(e *echo.Echo, t *testing.T) {
// Post
req := httptest.NewRequest(echo.POST, "/", bytes.NewReader(c1))
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetPath("/:key")
c.SetParamNames("key")
c.SetParamValues("c1")
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
// Assertions
if assert.NoError(t, addKeyWeights(c)) {
// t.Log(rec.Code)
assert.Equal(t, http.StatusCreated, rec.Code)
}
}
func TestWeights(t *testing.T) {
_init()
e := echo.New()
createOrUpdate(e, t)
result := map[string]int{}
jsonRes := map[string]string{}
for i := 0; i < 100*5; i++ {
// GET
req := httptest.NewRequest(echo.GET, "/", bytes.NewReader(c1))
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetPath("/:key")
c.SetParamNames("key")
c.SetParamValues("c1")
// try to simulate an update in between
if i%20 == 0 {
createOrUpdate(e, t)
}
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
// Assertions
if assert.NoError(t, getValue(c)) {
assert.Equal(t, http.StatusOK, rec.Code)
if err := json.Unmarshal([]byte(rec.Body.String()),
&jsonRes); err != nil {
assert.Error(t, err, nil)
}
result[jsonRes["key"]]++
}
}
assert.Equal(t, result["abc"], 10*5)
assert.Equal(t, result["bcd"], 15*5)
assert.Equal(t, result["cde"], 25*5)
assert.Equal(t, result["def"], 50*5)
}