iris/_examples/request-body/read-headers/main_test.go

52 lines
1.5 KiB
Go
Raw Normal View History

2020-08-31 20:28:13 +02:00
package main
import (
"testing"
"github.com/kataras/iris/v12/httptest"
)
func TestReadHeaders(t *testing.T) {
app := newApp()
e := httptest.New(t, app)
expectedOKBody := `myHeaders: main.myHeaders{RequestID:"373713f0-6b4b-42ea-ab9f-e2e04bc38e73", Authentication:"Bearer my-token"}`
tests := []struct {
headers map[string]string
code int
body string
2020-09-19 16:47:44 +02:00
regex bool
2020-08-31 20:28:13 +02:00
}{
{headers: map[string]string{
"X-Request-Id": "373713f0-6b4b-42ea-ab9f-e2e04bc38e73",
"Authentication": "Bearer my-token",
2020-09-19 16:47:44 +02:00
}, code: 200, body: expectedOKBody, regex: false},
2020-08-31 20:28:13 +02:00
{headers: map[string]string{
"x-request-id": "373713f0-6b4b-42ea-ab9f-e2e04bc38e73",
"authentication": "Bearer my-token",
2020-09-19 16:47:44 +02:00
}, code: 200, body: expectedOKBody, regex: false},
2020-08-31 20:28:13 +02:00
{headers: map[string]string{
2020-09-19 16:47:44 +02:00
"X-Request-Id": "373713f0-6b4b-42ea-ab9f-e2e04bc38e73",
2020-08-31 20:28:13 +02:00
"Authentication": "Bearer my-token",
2020-09-19 16:47:44 +02:00
}, code: 200, body: expectedOKBody, regex: false},
2020-08-31 20:28:13 +02:00
{headers: map[string]string{
"Authentication": "Bearer my-token",
2020-09-19 16:47:44 +02:00
}, code: 500, body: "X-Request-Id is empty", regex: false},
2020-08-31 20:28:13 +02:00
{headers: map[string]string{
2020-09-19 16:47:44 +02:00
"X-Request-Id": "373713f0-6b4b-42ea-ab9f-e2e04bc38e73",
}, code: 500, body: "Authentication is empty", regex: false},
{headers: map[string]string{}, code: 500, body: ".*\\(and 1 other error\\)$", regex: true},
2020-08-31 20:28:13 +02:00
}
for _, tt := range tests {
2020-09-19 16:47:44 +02:00
te := e.GET("/").WithHeaders(tt.headers).Expect().Status(tt.code).Body()
if tt.regex {
te.Match(tt.body)
} else {
te.Equal(tt.body)
}
2020-08-31 20:28:13 +02:00
}
}