MCPcopy Create free account
hub / github.com/gorilla/context / TestContext

Function TestContext

context_test.go:15–83  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

13)
14
15func TestContext(t *testing.T) {
16 assertEqual := func(val interface{}, exp interface{}) {
17 if val != exp {
18 t.Errorf("Expected %v, got %v.", exp, val)
19 }
20 }
21
22 r, _ := http.NewRequest("GET", "http://localhost:8080/", nil)
23 emptyR, _ := http.NewRequest("GET", "http://localhost:8080/", nil)
24
25 // Get()
26 assertEqual(Get(r, key1), nil)
27
28 // Set()
29 Set(r, key1, "1")
30 assertEqual(Get(r, key1), "1")
31 assertEqual(len(data[r]), 1)
32
33 Set(r, key2, "2")
34 assertEqual(Get(r, key2), "2")
35 assertEqual(len(data[r]), 2)
36
37 //GetOk
38 value, ok := GetOk(r, key1)
39 assertEqual(value, "1")
40 assertEqual(ok, true)
41
42 value, ok = GetOk(r, "not exists")
43 assertEqual(value, nil)
44 assertEqual(ok, false)
45
46 Set(r, "nil value", nil)
47 value, ok = GetOk(r, "nil value")
48 assertEqual(value, nil)
49 assertEqual(ok, true)
50
51 // GetAll()
52 values := GetAll(r)
53 assertEqual(len(values), 3)
54
55 // GetAll() for empty request
56 values = GetAll(emptyR)
57 if values != nil {
58 t.Error("GetAll didn't return nil value for invalid request")
59 }
60
61 // GetAllOk()
62 values, ok = GetAllOk(r)
63 assertEqual(len(values), 3)
64 assertEqual(ok, true)
65
66 // GetAllOk() for empty request
67 values, ok = GetAllOk(emptyR)
68 assertEqual(len(values), 0)
69 assertEqual(ok, false)
70
71 // Delete()
72 Delete(r, key1)

Callers

nothing calls this directly

Calls 7

GetFunction · 0.85
SetFunction · 0.85
GetOkFunction · 0.85
GetAllFunction · 0.85
GetAllOkFunction · 0.85
DeleteFunction · 0.85
ClearFunction · 0.85

Tested by

no test coverage detected