JSON Request

Methods http.post , http.put , http.delete automatically converts java.util.Map or java.util.List into application/json request Groovy http.post("/chat", [message: "hello", priority: "HIGH"]) { status.should == "SUCCESS" } Java http.post("/chat", http.json("message", "hello", "priority", "high"), (header, body) -> { body.get("status").should(equal("SUCCESS")); }); Note: For Java example uses a http.json shortcut but Map/List also works.

JSON Request From File

data/json#read-list Data JSON module has convenient methods to read JSON from resource/file as list/map/object Groovy http.post("/chat", data.json.map("chat-message.json")) { status.should == "SUCCESS" } Java http.post("/chat", data.json.map("chat-message.json"), (header, body) -> { body.get("status").should(equal("SUCCESS")); });

Generic Request

Use http.body to create generic body request. Groovy def content = binaryFileContent("path") http.post("/end-point", http.body("application/octet-stream", content)) { // assertions go here } Java byte[] content = binaryFileContent("path"); http.post("/end-point", http.body("application/octet-stream", content), (header, body) -> { // assertions go here });

Content-Type Shortcuts

WebTau provides shortcuts for Standard MIME types Groovy http.post("/end-point", http.application.json( "key1", "value1", "key2", "value2")) { // assertions go here } http.post("/end-point", http.application.json('{"key1": "value1", "key2": "value2"}')) { // assertions go here } def content = binaryFileContent("path") http.post("/end-point", http.application.octetStream(content)) { // assertions go here } def content = "text content" http.post("/end-point", http.text.plain(content)) { // assertions go here } Java http.post("/end-point", http.application.json( "key1", "value1", "key2", "value2"), (header, body) -> { // assertions go here }); http.post("/end-point", http.application.json("{\"key1\": \"value1\", \"key2\": \"value2\"}"), (header, body) -> { // assertions go here }); byte[] content = binaryFileContent("path"); http.post("/end-point", http.application.octetStream(content), (header, body) -> { // assertions go here }); String content = "text content"; http.post("/end-point", http.text.plain(content), (header, body) -> { // assertions go here }); Note: is a long form of and is there for completeness purpose. There is no behavior difference between passing an instance of java.util.Map and http.json

Form URL Encoded Data

Use http.formDataUrlEncoded to send application/x-www-form-urlencoded Groovy http.post("/submit", http.formDataUrlEncoded([firstName: "F Name", lastName: "L Name"])) { // ... } Java http.post("/submit", http.formDataUrlEncoded("firstName", "F Name", "lastName", "L Name"), (header, body) -> { // ... });

Form File Data

Consider example where backend expects a file as multipart/form-data . Field file defines content. Backend responds with file name and file description it received.Use http.formData to build a request body to send multipart/form-data , Groovy def imagePath = testResourcePath("src/test/resources/image.png") http.post("/file-upload", http.formData(file: imagePath)) { fileName.should == "image.png" } Java Path imagePath = testResourcePath("src/test/resources/image.png"); http.post("/file-upload", http.formData(map("file", imagePath)), (header, body) -> { body.get("fileName").should(equal("image.png")); }); Use http.formFile to override file name. Groovy def imagePath = testResourcePath("src/test/resources/image.png") http.post("/file-upload", http.formData(file: http.formFile("myFileName.png", imagePath))) { fileName.should == "myFileName.png" } Java Path imagePath = testResourcePath("src/test/resources/image.png"); http.post("/file-upload", http.formData(map( "file", http.formFile("myFileName.png", imagePath))), (header, body) -> { body.get("fileName").should(equal("myFileName.png")); }); Multiple form fields can be specified: Groovy def imagePath = testResourcePath("src/test/resources/image.png") http.post("/file-upload", http.formData(file: imagePath, fileDescription: "new report")) { fileName.should == "image.png" description.should == "new report" } Java Path imagePath = testResourcePath("src/test/resources/image.png"); http.post("/file-upload", http.formData(map( "file", imagePath, "fileDescription", "new report")), (header, body) -> { body.get("fileName").should(equal("image.png")); body.get("description").should(equal("new report")); }); To pass a file content directly, use Groovy byte[] fileContent = [1, 2, 3, 4] as byte[] http.post("/file-upload", http.formData(file: fileContent)) { fileName.should == "backend-generated-name-as-no-name-provided" } Java byte[] fileContent = new byte[] {1, 2, 3, 4}; http.post("/file-upload", http.formData(map("file", fileContent)), (header, body) -> { body.get("fileName").should(equal("backend-generated-name-as-no-name-provided")); }); Note: no file name is passed and this particular backend generated file name on your behalf.Use http.formFile to provide a file name Groovy byte[] fileContent = [1, 2, 3, 4] as byte[] http.post("/file-upload", http.formData( file: http.formFile("myFileName.dat", fileContent))) { fileName.should == "myFileName.dat" } Java byte[] fileContent = new byte[] {1, 2, 3, 4}; http.post("/file-upload", http.formData(map( "file", http.formFile("myFileName.dat", fileContent))), (header, body) -> { body.get("fileName").should(equal("myFileName.dat")); });

Parsed Response

Special HTTP/data-node Data Node body represents parsed response. Use it to validate response values. Groovy http.get("/query") { body.price.should == 100 } Note: For Groovy body is optional and when not specified, will be used implicitly http.get("/query") { price.should == 100 } Java http.get("/query", ((header, body) -> { body.get("price").should(equal(100)); }));

PDF Response

Use data.pdf.parse(body) to parse and assert PDF content from binary response. Groovy http.get("/report") { data.pdf.read(body).pageText(0).should contain("Quarterly earnings:") } Java http.get("/report", ((header, body) -> { data.pdf.read(body).pageText(0).should(contain("Quarterly earnings:")); })); Assign parse result to a local variable to make multiple assertions Groovy http.get("/report") { def pdf = data.pdf.read(body) pdf.pageText(0).should contain("Quarterly earnings:") pdf.pageText(1).should == "Intentional blank page\n" } Java http.get("/report", ((header, body) -> { Pdf pdf = data.pdf.read(body); pdf.pageText(0).should(contain("Quarterly earnings:")); pdf.pageText(1).should(equal("Intentional blank page\n")); })); Note: Use pdf assertions for sanity checks, i.e. presence of a correct client name or an account number. Implement comprehensive PDF generation logic tests as unit tests.

Raw Response

Use to access original text content Groovy def rawContent = http.post("/chat", [message: "hello world"]) { return body.getTextContent() } Java String rawContent = http.post("/chat", http.json("message", "hello world"), (header, body) -> { return body.getTextContent(); });