-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1258 - Add convenience methods to controller specs to work with JSON
- Loading branch information
Showing
7 changed files
with
183 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
activeweb-testing/src/test/java/app/controllers/JSONController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package app.controllers; | ||
|
||
import org.javalite.activeweb.AppController; | ||
import org.javalite.activeweb.annotations.POST; | ||
|
||
import org.javalite.json.JSONMap; | ||
|
||
public class JSONController extends AppController { | ||
|
||
@POST | ||
public void index1(){ | ||
JSONMap request = getRequestJSONMap(); | ||
request.put("last_name", "Doe"); | ||
respondJSON(request); | ||
} | ||
|
||
@POST | ||
public void index2(){ | ||
respondJSON(getRequestJSONList()); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
activeweb-testing/src/test/java/app/controllers/JSONControllerSpec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package app.controllers; | ||
|
||
import org.javalite.activeweb.ControllerSpec; | ||
import org.javalite.json.JSONList; | ||
import org.javalite.json.JSONMap; | ||
import org.junit.Test; | ||
|
||
public class JSONControllerSpec extends ControllerSpec { | ||
|
||
@Test | ||
public void shouldPostJSONMap(){ | ||
|
||
String doc = """ | ||
{ | ||
"first_name" : "John" | ||
} | ||
"""; | ||
request().json(doc).post("index1"); | ||
|
||
JSONMap response = responseJSONMap(); | ||
|
||
the(response).shouldContain("first_name"); | ||
the(response).shouldContain("last_name"); | ||
|
||
the(response.get("first_name")).shouldContain("John"); | ||
the(response.get("last_name")).shouldContain("Doe"); | ||
|
||
the(contentType()).shouldBeEqual("application/json"); | ||
} | ||
|
||
@Test | ||
public void shouldPostJSONList(){ | ||
|
||
String doc = """ | ||
[ | ||
"John" , "Doe" | ||
] | ||
"""; | ||
request().json(doc).post("index2"); | ||
|
||
JSONList response = responseJSONList(); | ||
|
||
the(response).shouldContain("John"); | ||
the(response).shouldContain("Doe"); | ||
the(contentType()).shouldBeEqual("application/json"); | ||
} | ||
|
||
@Test | ||
public void shouldPostJSONString(){ | ||
|
||
String doc = """ | ||
[ | ||
"John" , "Doe" | ||
] | ||
"""; | ||
request().json(doc).post("index2"); | ||
|
||
JSONList response = responseJSONList(); | ||
|
||
the(response).shouldContain("John"); | ||
the(response).shouldContain("Doe"); | ||
the(contentType()).shouldBeEqual("application/json"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters