-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator_test.go
More file actions
119 lines (101 loc) · 4.31 KB
/
Copy pathvalidator_test.go
File metadata and controls
119 lines (101 loc) · 4.31 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package berrors
import (
"github.com/valyala/fasthttp"
"net/http"
"testing"
"github.com/gofiber/fiber/v2"
)
type testStruct struct {
Name string `json:"name" validate:"required"`
Email string `json:"email,omitempty" validate:"required,email"`
}
func TestGetJSONFieldName(t *testing.T) {
testData := testStruct{}
// test with an incorrect struct field
incorrectResult, err := GetJSONFieldName(&testData, "Size")
if err == nil || incorrectResult != "" {
t.Errorf("GetJSONFieldName should return an error.")
}
// test with an correct struct field
correctResult, err := GetJSONFieldName(&testData, "Name")
if err != nil {
t.Errorf("GetJSONFieldName shouldn't return an error. Err: %s\n", err)
}
if correctResult != "name" {
t.Errorf("GetJSONFieldName should return `name`. Got: %s\n", correctResult)
}
// test with omitempty
omitemptyResult, err := GetJSONFieldName(&testData, "Email")
if err != nil {
t.Errorf("GetJSONFieldName shouldn't return an error. Err: %s\n", err)
}
if omitemptyResult != "email" {
t.Errorf("GetJSONFieldName should return `email`. Got: %s\n", omitemptyResult)
}
}
func TestParseBodyJSONCorrectData(t *testing.T) {
testData := new(testStruct)
expectedData := testStruct{
Name: "John Doe",
Email: "test@test.com",
}
fiberApp := fiber.New()
ctx := fiberApp.AcquireCtx(&fasthttp.RequestCtx{})
ctx.Context().Request.Header.SetContentType("application/json")
ctx.Context().Request.SetBodyString("{\"name\":\"John Doe\",\"email\":\"test@test.com\"}")
ok := ParseBodyJSON(ctx, testData)
if !ok {
t.Errorf("ParseBodyJSON should return true. Got: %t\n", ok)
}
if testData.Name != expectedData.Name || testData.Email != expectedData.Email {
t.Errorf("testData from ParseBodyJSON does not contain the expected data.\nGot: \n%+v\nExpected: \n%+v\n", *testData, expectedData)
}
}
func TestParseBodyJSONMissingRequiredData(t *testing.T) {
testData := new(testStruct)
expectedError := "{\"fields\":[{\"name\":\"name\",\"reason\":\"The field is required\"}]}"
fiberApp := fiber.New()
ctx := fiberApp.AcquireCtx(&fasthttp.RequestCtx{})
ctx.Context().Request.Header.SetContentType("application/json")
ctx.Context().Request.SetBodyString("{\"email\":\"test@test.com\"}")
ok := ParseBodyJSON(ctx, testData)
if ok {
t.Errorf("ParseBodyJSON should return false. Got: %t\n", ok)
}
if ctx.Context().Response.Header.StatusCode() != http.StatusBadRequest ||
string(ctx.Context().Response.Body()) != expectedError {
t.Errorf("Http error should be:\n%d: %s\ngot:\n%d: %s\n", http.StatusBadRequest, expectedError, ctx.Context().Response.Header.StatusCode(), string(ctx.Context().Response.Body()))
}
}
func TestParseBodyJSONIncorrectEmail(t *testing.T) {
testData := new(testStruct)
expectedError := "{\"fields\":[{\"name\":\"email\",\"reason\":\"The email given is not correct\"}]}"
fiberApp := fiber.New()
ctx := fiberApp.AcquireCtx(&fasthttp.RequestCtx{})
ctx.Context().Request.Header.SetContentType("application/json")
ctx.Context().Request.SetBodyString("{\"name\":\"John Doe\",\"email\":\"test.com\"}")
ok := ParseBodyJSON(ctx, testData)
if ok {
t.Errorf("ParseBodyJSON should return false. Got: %t\n", ok)
}
if ctx.Context().Response.Header.StatusCode() != http.StatusBadRequest ||
string(ctx.Context().Response.Body()) != expectedError {
t.Errorf("Http error should be:\n%d: %s\ngot:\n%d: %s\n", http.StatusBadRequest, expectedError, ctx.Context().Response.Header.StatusCode(), string(ctx.Context().Response.Body()))
}
}
func TestParseBodyJSONIncorrectJSON(t *testing.T) {
testData := new(testStruct)
expectedError := "json: cannot unmarshal \"2,\\\"email\\\":\\\"test@test.com\\\"}\" into Go struct field berrors.testStruct.name. of type string"
fiberApp := fiber.New()
ctx := fiberApp.AcquireCtx(&fasthttp.RequestCtx{})
ctx.Context().Request.Header.SetContentType("application/json")
ctx.Context().Request.SetBodyString("{\"name\":2,\"email\":\"test@test.com\"}")
ok := ParseBodyJSON(ctx, testData)
if ok {
t.Errorf("ParseBodyJSON should return false. Got: %t\n", ok)
}
if ctx.Context().Response.Header.StatusCode() != http.StatusBadRequest ||
string(ctx.Context().Response.Body()) != expectedError {
t.Errorf("Http error should be:\n%d: %s\ngot:\n%d: %s\n", http.StatusBadRequest, expectedError, ctx.Context().Response.Header.StatusCode(), string(ctx.Context().Response.Body()))
}
}