-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_test.go
104 lines (86 loc) · 2.94 KB
/
response_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
92
93
94
95
96
97
98
99
100
101
102
103
104
package apirouter
import (
"bytes"
"encoding/json"
"testing"
)
// TestJsonEncode tests the json encoder removes fields that are not approved
func TestJsonEncode(t *testing.T) {
t.Parallel()
// Set up a mock struct for testing
type TestStruct struct {
TestKey string `json:"test_key"`
TestKeyTwo string `json:"test_key_two"`
notAllowed string
}
// Base model and test model
var model = new(TestStruct)
var modelTest = new(TestStruct)
var allowedFields = []string{"test_key", "test_key_two"} // notice omitted: notAllowed
// Set the testing data
model.TestKey = "TestValue1"
model.TestKeyTwo = "TestValue2"
model.notAllowed = "PrivateValue"
// Set the buffer and encoder
var b bytes.Buffer
enc := json.NewEncoder(&b)
// Run the encoder
err := JSONEncode(enc, model, allowedFields)
if err != nil {
t.Fatal(err)
}
// Now unmarshal and test
if err = json.Unmarshal(b.Bytes(), &modelTest); err != nil {
t.Fatal(err)
}
// Test for our fields and values now
if modelTest.TestKey != "TestValue1" {
t.Fatal("TestKey does not have the right value! Encoding failed.", modelTest.TestKey)
} else if modelTest.TestKeyTwo != "TestValue2" {
t.Fatal("TestKeyTwo does not have the right value! Encoding failed.", modelTest.TestKeyTwo)
} else if modelTest.notAllowed == "PrivateValue" {
t.Fatal("Field not removed! notAllowed does not have the right value! Encoding failed.", modelTest.notAllowed)
}
}
// TestJsonEncodeSubstruct tests the json encoder removes fields that are not approved
func TestJsonEncodeSubstruct(t *testing.T) {
t.Parallel()
// Set up a new mock substruct
type TestSubStruct struct {
TestSubKey string `json:"test_sub_key"`
}
// Set up a mock struct for testing
type TestStruct struct {
TestKey string `json:"test_key"`
TestKeyTwo TestSubStruct `json:"test_key_two"`
NotAllowed string `json:"not_allowed"`
}
// Base model and test model
var model = new(TestStruct)
var modelTest = new(TestStruct)
var allowedFields = []string{"test_key", "test_key_two"} // notice omitted: notAllowed
// Set the testing data
model.TestKey = "TestValue1"
model.TestKeyTwo.TestSubKey = "TestSubValue"
model.NotAllowed = "PrivateValue"
// Set the buffer and encoder
var b bytes.Buffer
enc := json.NewEncoder(&b)
// Run the encoder
err := JSONEncode(enc, model, allowedFields)
if err != nil {
t.Fatal(err)
}
// Now unmarshal and test
if err = json.Unmarshal(b.Bytes(), &modelTest); err != nil {
t.Fatal(err)
}
// Test for our fields and values now
if modelTest.TestKey != "TestValue1" {
t.Fatal("TestKey does not have the right value! Encoding failed.", modelTest.TestKey)
} else if modelTest.TestKeyTwo.TestSubKey != "TestSubValue" {
t.Fatal("TestKeyTwo does not have the right value! Encoding failed.", modelTest.TestKeyTwo)
} else if modelTest.NotAllowed == "PrivateValue" {
t.Fatal("Field not removed! notAllowed does not have the right value! Encoding failed.", modelTest.NotAllowed)
}
}