Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests to check support for application/x-www-form-urlencoded MediaType #666

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package io.micronaut.gcp.function.http


import io.micronaut.context.annotation.Property
import io.micronaut.http.HttpHeaders
import io.micronaut.http.HttpMethod
import io.micronaut.http.HttpStatus
import io.micronaut.http.MediaType
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import spock.lang.PendingFeature
import spock.lang.Specification

@MicronautTest
@Property(name = 'spec.name', value = 'HttpContentTypeSpec')
class HttpContentTypeSpec extends Specification {

@PendingFeature(reason = "failing with BAD_REQUEST")
void 'POST form url encoded body binding to pojo works'() {
given:
def function = new HttpFunction()
def googleResponse = initMockResponse()
def googleRequest = new MockGoogleRequest(HttpMethod.POST, "/form", 'message=World')

when:
googleRequest.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED)
function.service(googleRequest, googleResponse)

then:
googleResponse.contentType.get() == MediaType.APPLICATION_JSON
googleResponse.statusCode == HttpStatus.OK.code
googleResponse.text == '{"message":"Hello World"}'

cleanup:
function.close()
}

@PendingFeature(reason = "failing with BAD_REQUEST")
void "POST form url encoded body binding to pojo works if you don't specify body annotation"() {
given:
def function = new HttpFunction()
def googleResponse = initMockResponse()
def googleRequest = new MockGoogleRequest(HttpMethod.POST, "/form/without-body-annotation", 'message=World')

when:
googleRequest.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED)
function.service(googleRequest, googleResponse)

then:
googleResponse.contentType.get() == MediaType.APPLICATION_JSON
googleResponse.statusCode == HttpStatus.OK.code
googleResponse.text == '{"message":"Hello World"}'

cleanup:
function.close()
}

@PendingFeature(reason = "failing with BAD_REQUEST")
void "POST form-url-encoded with Body annotation and a nested attribute"() {
given:
def function = new HttpFunction()
def googleResponse = initMockResponse()
def googleRequest = new MockGoogleRequest(HttpMethod.POST, "/form/nested-attribute", 'message=World')

when:
googleRequest.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED)
function.service(googleRequest, googleResponse)

then:
googleResponse.contentType.get() == MediaType.APPLICATION_JSON
googleResponse.statusCode == HttpStatus.OK.code
googleResponse.text == '{"message":"Hello World"}'

cleanup:
function.close()
}

void "POST application-json with Body annotation and a nested attribute"() {
given:
def function = new HttpFunction()
def googleResponse = initMockResponse()
def googleRequest = new MockGoogleRequest(HttpMethod.POST, "/form/json-nested-attribute", '{"message":"World"}')

when:
googleRequest.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
function.service(googleRequest, googleResponse)

then:
googleResponse.contentType.get() == MediaType.APPLICATION_JSON
googleResponse.statusCode == HttpStatus.OK.code
googleResponse.text == '{"message":"Hello World"}'

cleanup:
function.close()
}

@PendingFeature(reason = "failing with BAD_REQUEST")
// Cannot convert type [class io.micronaut.core.convert.value.ConvertibleValuesMap] to target type: class io.micronaut.gcp.function.http.MessageCreate
void "POST application-json without Body annotation"() {
given:
def function = new HttpFunction()
def googleResponse = initMockResponse()
def googleRequest = new MockGoogleRequest(HttpMethod.POST, "/form/json-without-body-annotation", '{"message":"World"}')

when:
googleRequest.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
function.service(googleRequest, googleResponse)

then:
googleResponse.contentType.get() == MediaType.APPLICATION_JSON
googleResponse.statusCode == HttpStatus.OK.code
googleResponse.text == '{"message":"Hello World"}'

cleanup:
function.close()
}

void "POST application-json with Body annotation and a nested attribute and Map return rendered as JSON"() {
given:
def function = new HttpFunction()
def googleResponse = initMockResponse()
def googleRequest = new MockGoogleRequest(HttpMethod.POST, "/form/json-nested-attribute-with-map-return", '{"message":"World"}')

when:
googleRequest.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
function.service(googleRequest, googleResponse)

then:
googleResponse.contentType.get() == MediaType.APPLICATION_JSON
googleResponse.statusCode == HttpStatus.OK.code
googleResponse.text == '{"message":"Hello World"}'

cleanup:
function.close()
}

void "POST application-json with Body annotation and Object return rendered as JSON"() {
given:
def function = new HttpFunction()
def googleResponse = initMockResponse()
def googleRequest = new MockGoogleRequest(HttpMethod.POST, "/form/json-with-body-annotation-and-with-object-return", '{"message":"World"}')

when:
googleRequest.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
function.service(googleRequest, googleResponse)

then:
googleResponse.contentType.get() == MediaType.APPLICATION_JSON
googleResponse.statusCode == HttpStatus.OK.code
googleResponse.text == '{"greeting":"Hello World"}'

cleanup:
function.close()
}

void "POST json with @Body annotation"() {
given:
def function = new HttpFunction()
def googleResponse = initMockResponse()
def googleRequest = new MockGoogleRequest(HttpMethod.POST, "/form/json-with-body", '{"message":"World"}')

when:
googleRequest.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
function.service(googleRequest, googleResponse)

then:
googleResponse.contentType.get() == MediaType.APPLICATION_JSON
googleResponse.statusCode == HttpStatus.OK.code
googleResponse.text == '{"message":"Hello World"}'

cleanup:
function.close()
}

private MockGoogleResponse initMockResponse() {
MockGoogleResponse response = new MockGoogleResponse()
response.setContentType(MediaType.APPLICATION_JSON)
return response
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.micronaut.gcp.function.http;

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Consumes;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Post;

import java.util.Collections;
import java.util.Map;

@Controller("/form")
public class FormController {
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Post
String save(@Body MessageCreate messageCreate) {
return "{\"message\":\"Hello " + messageCreate.getMessage() + "\"}";
}

@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Post("/without-body-annotation")
String withoutBodyAnnotation(MessageCreate messageCreate) {
return "{\"message\":\"Hello " + messageCreate.getMessage() + "\"}";
}

@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Post("/nested-attribute")
String save(@Body("message") String value) {
return "{\"message\":\"Hello " + value + "\"}";
}

@Consumes(MediaType.APPLICATION_JSON)
@Post("/json-nested-attribute")
String jsonNestedAttribute(@Body("message") String value) {
return "{\"message\":\"Hello " + value + "\"}";
}

@Consumes(MediaType.APPLICATION_JSON)
@Post("/json-without-body-annotation")
String jsonWithoutBody(MessageCreate messageCreate) {
return "{\"message\":\"Hello " + messageCreate.getMessage() + "\"}";
}

@Consumes(MediaType.APPLICATION_JSON)
@Post("/json-nested-attribute-with-map-return")
Map<String, String> jsonNestedAttributeWithMapReturn(@Body("message") String value) {
return Collections.singletonMap("message", "Hello " + value);
}

@Consumes(MediaType.APPLICATION_JSON)
@Post("/json-with-body-annotation-and-with-object-return")
MyResponse jsonNestedAttributeWithObjectReturn(@Body MessageCreate messageCreate) {
return new MyResponse("Hello " + messageCreate.getMessage());
}

@Consumes(MediaType.APPLICATION_JSON)
@Post("/json-with-body")
String jsonWithBody(@Body MessageCreate messageCreate) {
return "{\"message\":\"Hello " + messageCreate.getMessage() + "\"}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.micronaut.gcp.function.http;

import io.micronaut.core.annotation.Introspected;
import io.micronaut.core.annotation.NonNull;

import javax.validation.constraints.NotBlank;

@Introspected
public class MessageCreate {

@NonNull
@NotBlank
private final String message;

public MessageCreate(@NonNull String message) {
this.message = message;
}

@NonNull
public String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.micronaut.gcp.function.http;

import io.micronaut.core.annotation.Introspected;
import io.micronaut.core.annotation.NonNull;

import javax.validation.constraints.NotBlank;

@Introspected
public class MyResponse {
@NonNull
@NotBlank
private final String greeting;

public MyResponse(@NonNull String greeting) {
this.greeting = greeting;
}

@NonNull
public String getGreeting() {
return greeting;
}
}