Skip to content

Commit fdc4e60

Browse files
committed
additional solution
1 parent 8e24452 commit fdc4e60

File tree

3 files changed

+100
-3
lines changed

3 files changed

+100
-3
lines changed

challenger/src/main/java/uk/co/compendiumdev/challenge/challenges/definitions/PostChallenges.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,12 @@ public static ChallengeDefinitionData postTodosInvalidExtraField400(int challeng
113113
"Issue a POST request to create a todo but fail validation because your payload contains an unrecognised field.");
114114

115115
aChallenge.addHint("Try to create a todo with a title, description and a priority");
116-
// aChallenge.addSolutionLink("Read Solution", "HREF", "https://www.eviltester.com/apichallenges/howto/post-todos-400");
116+
117+
aChallenge.addSolutionLink("Send a POST request to /todos with a priority field e.g. {\"title\":\"a title\",\"priority\":\"extra\"}", "", "");
118+
aChallenge.addSolutionLink("Read Solution", "HREF", "/apichallenges/solutions/post-create/post-todos-400-extra-field");
119+
117120
// aChallenge.addSolutionLink("Watch Insomnia Solution", "YOUTUBE", "tlye5bQ72g0");
118-
// TODO: create solution for unrecognised field names
121+
// TODO: create video solution for unrecognised field names
119122
return aChallenge;
120123
}
121124

challenger/src/main/resources/content/apichallenges/solutions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ description: A list of all the solutions for the API Challenges. Try them yourse
3333
- [POST /todos (400) description too long](/apichallenges/solutions/post-create/post-todos-400-description-too-long)
3434
- [POST /todos (201) max out content](/apichallenges/solutions/post-create/post-todos-201-max-content)
3535
- [POST /todos (413) content too long](/apichallenges/solutions/post-create/post-todos-413-content-too-long)
36-
- POST /todos (400) extra
36+
- [POST /todos (400) extra](/apichallenges/solutions/post-create/post-todos-400-extra-field)
3737

3838
## Creation Challenges with PUT
3939

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
date: 2021-01-30T15:55:00Z
3+
title: API Challenges Solution For - POST todos 400 extra
4+
description: How to solve API challenge POST todos 400 extra to trigger validation errors due to an extra field in the payload.
5+
---
6+
7+
# How to complete the challenge `POST /todos (400) extra`
8+
9+
How to complete the challenge `POST /todos (400) extra` to fail to create a todo item in the application due to not passing validation when the payload contains an extra field.
10+
11+
## POST /todos (400) extra
12+
13+
> Issue a POST request to create a todo but fail validation due to an unrecognised field
14+
15+
- `POST` request will create a todo if the details are valid when using the `/todos` end point
16+
- `400` is an error code meaning that we supplied invalid details
17+
- In this case we are asked to make a mistake by adding an extra field not defined in the request schema e.g. `priority="extra"`
18+
19+
## Basic Instructions
20+
21+
- Issue a `POST` request to end point "/todos"
22+
- `{{<ORIGIN_URL>}}/todos`
23+
- The request should have an `X-CHALLENGER` header to track challenge completion
24+
- The `content-type` in the message should be `application/json` because we are sending a JSON payload
25+
- The Payload should have an error due to an unexpected field.
26+
27+
```json
28+
{
29+
"title": "a title",
30+
"priority": "extra"
31+
}
32+
```
33+
- The response status code should be `400` because the request is invalid
34+
- The body of the response will be an error message array with a single message
35+
36+
```json
37+
{
38+
"errorMessages": [
39+
"Could not find field: priority"
40+
]
41+
}
42+
```
43+
44+
Hints:
45+
46+
- We don't just want to check for mandatory and missing content we need to make sure that the server does not try and create entities and inject new fields into the database
47+
- For follow on exercises you might want to see what happens:
48+
- if we duplicate fields e.g. have two `title` fields
49+
- if we duplicate headers
50+
- When testing APIS we need to go beyond field contents and look at the message format itself
51+
52+
## Example Request
53+
54+
~~~~~~~~
55+
> POST /todos HTTP/1.1
56+
> Host: {{<HOST_URL>}}
57+
> User-Agent: rest-client
58+
> X-CHALLENGER: x-challenger-guid
59+
> Content-Type: application/json
60+
> Accept: */*
61+
> Content-Length: 116
62+
63+
| {
64+
| "title": "a title",
65+
| "priority": "extra"
66+
| }
67+
~~~~~~~~
68+
69+
## Example Response
70+
71+
~~~~~~~~
72+
< HTTP/1.1 400 Bad Request
73+
< Connection: close
74+
< Date: Thu, 27 Aug 2020 14:23:12 GMT
75+
< Content-Type: application/json
76+
< X-Challenger: x-challenger-guid
77+
< Server: Jetty(9.4.z-SNAPSHOT)
78+
< Via: 1.1 vegur
79+
~~~~~~~~
80+
81+
Returned body:
82+
83+
```json
84+
{
85+
"errorMessages": [
86+
"Could not find field: priority"
87+
]
88+
}
89+
```
90+
91+
92+
93+
94+

0 commit comments

Comments
 (0)