1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-22 04:17:03 +00:00

add read-body example

Former-commit-id: 7d7afa555e66fbe7edfa31203c26b4b550f336f4
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-04-11 00:55:31 +03:00
parent 978718454a
commit e9b10b14a3
4 changed files with 111 additions and 2 deletions

View File

@@ -0,0 +1,53 @@
package main
import (
"testing"
"github.com/kataras/iris/v12/httptest"
)
func TestReadBodyAndNegotiate(t *testing.T) {
app := newApp()
e := httptest.New(t, app)
var (
expectedPayload = payload{Message: "a message"}
expectedMsgPackPayload = "\x81\xa7message\xa9a message"
expectedXMLPayload = `<payload>
<message>a message</message>
</payload>
`
expectedYAMLPayload = "Message: a message\n"
)
// Test send JSON and receive JSON.
e.POST("/").WithJSON(expectedPayload).Expect().Status(httptest.StatusOK).
JSON().Equal(expectedPayload)
// Test send Form and receive XML.
e.POST("/").WithForm(expectedPayload).
WithHeader("Accept", "application/xml").
Expect().Status(httptest.StatusOK).
Body().Equal(expectedXMLPayload)
// Test send URL Query and receive MessagePack.
e.POST("/").WithQuery("message", expectedPayload.Message).
WithHeader("Accept", "application/msgpack").
Expect().Status(httptest.StatusOK).ContentType("application/msgpack").
Body().Equal(expectedMsgPackPayload)
// Test send MessagePack and receive MessagePack.
e.POST("/").WithBytes([]byte(expectedMsgPackPayload)).
WithHeader("Content-Type", "application/msgpack").
WithHeader("Accept", "application/msgpack").
Expect().Status(httptest.StatusOK).
ContentType("application/msgpack").Body().Equal(expectedMsgPackPayload)
// Test send YAML and receive YAML.
e.POST("/").WithBytes([]byte(expectedYAMLPayload)).
WithHeader("Content-Type", "application/x-yaml").
WithHeader("Accept", "application/x-yaml").
Expect().Status(httptest.StatusOK).
ContentType("application/x-yaml").Body().Equal(expectedYAMLPayload)
}