mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-12-25 04:43:03 +03:00
Add test case for handling multiple content types.
This commit is contained in:
parent
ad5f092d06
commit
6e53ce5f74
@ -156,3 +156,38 @@ it("gives an error when there is no content-type header", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it("handles XML body", () => {
|
||||
cy.request({
|
||||
method: "POST",
|
||||
url: "/api/multiple-content-types",
|
||||
headers: { "Content-Type": "application/xml ; charset=utf-8" },
|
||||
body: `
|
||||
<root>
|
||||
<path>
|
||||
<to>
|
||||
<string>
|
||||
<value>SomeString</value>
|
||||
</string>
|
||||
</to>
|
||||
</path>
|
||||
</root>
|
||||
`,
|
||||
}).then((res) => {
|
||||
expect(res.headers["content-type"]).to.eq("text/plain");
|
||||
expect(res.status).to.eq(200);
|
||||
expect(res.body).to.eq("SomeString");
|
||||
});
|
||||
});
|
||||
|
||||
it("handles JSON body", () => {
|
||||
cy.request({
|
||||
method: "POST",
|
||||
url: "/api/multiple-content-types",
|
||||
body: { path: { to: { string: { value: "SomeString" } } } },
|
||||
}).then((res) => {
|
||||
expect(res.headers["content-type"]).to.eq("text/plain");
|
||||
expect(res.status).to.eq(200);
|
||||
expect(res.body).to.eq("SomeString");
|
||||
});
|
||||
});
|
||||
|
@ -41,6 +41,7 @@ routes getStaticRoutes htmlToString =
|
||||
|> ApiRoute.serverRender
|
||||
, requestPrinter
|
||||
, xmlDecoder
|
||||
, multipleContentTypes
|
||||
]
|
||||
|
||||
|
||||
@ -69,6 +70,40 @@ xmlDecoder =
|
||||
|> ApiRoute.serverRender
|
||||
|
||||
|
||||
multipleContentTypes : ApiRoute ApiRoute.Response
|
||||
multipleContentTypes =
|
||||
let
|
||||
dataDecoder : Xml.Decode.Decoder String
|
||||
dataDecoder =
|
||||
Xml.Decode.path [ "path", "to", "string", "value" ] (Xml.Decode.single Xml.Decode.string)
|
||||
in
|
||||
ApiRoute.succeed
|
||||
(Request.oneOf
|
||||
[ Request.map2
|
||||
(\_ xmlString ->
|
||||
xmlString
|
||||
|> Xml.Decode.run dataDecoder
|
||||
|> Result.Extra.merge
|
||||
|> Response.plainText
|
||||
|> DataSource.succeed
|
||||
)
|
||||
(Request.expectContentType "application/xml")
|
||||
Request.expectBody
|
||||
, Request.map
|
||||
(\decodedValue ->
|
||||
decodedValue
|
||||
|> Response.plainText
|
||||
|> DataSource.succeed
|
||||
)
|
||||
(Request.expectJsonBody (Decode.at [ "path", "to", "string", "value" ] Decode.string))
|
||||
]
|
||||
)
|
||||
|> ApiRoute.literal "api"
|
||||
|> ApiRoute.slash
|
||||
|> ApiRoute.literal "multiple-content-types"
|
||||
|> ApiRoute.serverRender
|
||||
|
||||
|
||||
requestPrinter : ApiRoute ApiRoute.Response
|
||||
requestPrinter =
|
||||
ApiRoute.succeed
|
||||
|
Loading…
Reference in New Issue
Block a user