mirror of
https://github.com/kataras/iris.git
synced 2026-01-08 20:41:57 +00:00
add Context.ReadHeaders
This commit is contained in:
@@ -138,6 +138,7 @@
|
||||
* [Bind YAML](request-body/read-yaml/main.go)
|
||||
* [Bind Form](request-body/read-form/main.go)
|
||||
* [Bind Query](request-body/read-query/main.go)
|
||||
* [Bind Headers](request-body/read-headers/main.go)
|
||||
* [Bind Params](request-body/read-params/main.go)
|
||||
* [Bind Body](request-body/read-body/main.go)
|
||||
* [Bind Custom per type](request-body/read-custom-per-type/main.go)
|
||||
|
||||
41
_examples/request-body/read-headers/main.go
Normal file
41
_examples/request-body/read-headers/main.go
Normal file
@@ -0,0 +1,41 @@
|
||||
// package main contains an example on how to use the ReadHeaders,
|
||||
// same way you can do the ReadQuery, ReadJSON, ReadProtobuf and e.t.c.
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
type myHeaders struct {
|
||||
RequestID string `header:"X-Request-Id,required"`
|
||||
Authentication string `header:"Authentication,required"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
|
||||
// http://localhost:8080
|
||||
/*
|
||||
myHeaders: main.myHeaders{
|
||||
RequestID: "373713f0-6b4b-42ea-ab9f-e2e04bc38e73",
|
||||
Authentication: "Bearer my-token",
|
||||
}
|
||||
*/
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
var hs myHeaders
|
||||
if err := ctx.ReadHeaders(&hs); err != nil {
|
||||
ctx.StopWithError(iris.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Writef("myHeaders: %#v", hs)
|
||||
})
|
||||
|
||||
return app
|
||||
}
|
||||
45
_examples/request-body/read-headers/main_test.go
Normal file
45
_examples/request-body/read-headers/main_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
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
|
||||
}{
|
||||
{headers: map[string]string{
|
||||
"X-Request-Id": "373713f0-6b4b-42ea-ab9f-e2e04bc38e73",
|
||||
"Authentication": "Bearer my-token",
|
||||
}, code: 200, body: expectedOKBody},
|
||||
{headers: map[string]string{
|
||||
"x-request-id": "373713f0-6b4b-42ea-ab9f-e2e04bc38e73",
|
||||
"authentication": "Bearer my-token",
|
||||
}, code: 200, body: expectedOKBody},
|
||||
{headers: map[string]string{
|
||||
"X-Request-ID": "373713f0-6b4b-42ea-ab9f-e2e04bc38e73",
|
||||
"Authentication": "Bearer my-token",
|
||||
}, code: 200, body: expectedOKBody},
|
||||
{headers: map[string]string{
|
||||
"Authentication": "Bearer my-token",
|
||||
}, code: 500, body: "X-Request-Id is empty"},
|
||||
{headers: map[string]string{
|
||||
"X-Request-ID": "373713f0-6b4b-42ea-ab9f-e2e04bc38e73",
|
||||
}, code: 500, body: "Authentication is empty"},
|
||||
{headers: map[string]string{}, code: 500, body: "X-Request-Id is empty (and 1 other error)"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
e.GET("/").WithHeaders(tt.headers).Expect().Status(tt.code).Body().Equal(tt.body)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user