Skip to content

Commit

Permalink
Merge pull request #15 from jupiter-tools/return-response-api
Browse files Browse the repository at this point in the history
Return response api
  • Loading branch information
antkorwin authored Apr 8, 2020
2 parents 80081b6 + 2816788 commit 8e76489
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.jupiter-tools</groupId>
<artifactId>mvc-requester</artifactId>
<version>0.2</version>
<version>0.3</version>
<packaging>jar</packaging>

<name>mvc-requester</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.http.HttpStatus;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;

Expand Down Expand Up @@ -123,4 +124,17 @@ public <ResultType> ResultType returnAsPrimitive(Class<ResultType> returnType) {
return isBlank(body) ? null : (ResultType) PrimitiveConverter.convertToPrimitive(body, returnType);
});
}


/**
* Return a plain received response of the REST-API invocation
*
* @return MockHttpServletResponse
*/
public MockHttpServletResponse returnResponse() {
return wrap(() -> {
resultActions.andDo(print());
return resultActions.andReturn().getResponse();
});
}
}
70 changes: 47 additions & 23 deletions src/test/java/com/jupiter/tools/mvc/requester/MvcRequesterTest.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
package com.jupiter.tools.mvc.requester;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.http.HttpStatus.OK;

/**
* Created on 30.01.2019.
Expand All @@ -45,33 +53,33 @@ void setUp() {
}

@Test
void expectStatusAndReturn() throws Exception {
void expectStatusAndReturn() {
// Act
String result = MvcRequester.on(mockMvc)
.to("/test/hello")
.get()
// Assert
.expectStatus(HttpStatus.OK)
.expectStatus(OK)
.returnAsPrimitive(String.class);

assertThat(result).isEqualTo("hello world");
}

@Test
void returnAsPrimitiveToInteger() throws Exception {
void returnAsPrimitiveToInteger() {
// Act
Integer result = MvcRequester.on(mockMvc)
.to("/test/integer")
.get()
// Assert
.expectStatus(HttpStatus.OK)
.expectStatus(OK)
.returnAsPrimitive(Integer.class);

assertThat(result).isEqualTo(42);
}

@Test
void testCreate() throws Exception {
void testCreate() {
// Arrange
// Act
MvcRequester.on(mockMvc)
Expand All @@ -82,7 +90,7 @@ void testCreate() throws Exception {
}

@Test
void testUrlTrim() throws Exception {
void testUrlTrim() {
// Arrange
// Act
MvcRequester.on(mockMvc)
Expand All @@ -93,7 +101,7 @@ void testUrlTrim() throws Exception {
}

@Test
void testAppendSlashInBeginOfUrl() throws Exception {
void testAppendSlashInBeginOfUrl() {
// Arrange
// Act
MvcRequester.on(mockMvc)
Expand All @@ -107,9 +115,9 @@ void testAppendSlashInBeginOfUrl() throws Exception {
void testEmptyResponse() {
// Act
String res = MvcRequester.on(mockMvc)
.to("test/create")
.post()
.returnAsPrimitive(String.class);
.to("test/create")
.post()
.returnAsPrimitive(String.class);
// Assert
assertThat(res).isNull();
}
Expand Down Expand Up @@ -144,16 +152,16 @@ void expectHttpStatus() {
.to("/test/hello")
.get()
// Assert
.expectStatus(HttpStatus.OK);
.expectStatus(OK);
}

@Test
void expectWithWrongStatusMustThrowAssertionError() {
Assertions.assertThrows(AssertionError.class,
() -> MvcRequester.on(mockMvc)
.to("/test/error")
.get()
.expectStatus(HttpStatus.OK));
.to("/test/error")
.get()
.expectStatus(OK));
}

@Test
Expand All @@ -165,44 +173,60 @@ void doExpectTest() {
}

@Test
void skipUrlPrefix() throws Exception {
void skipUrlPrefix() {
// Act
String result = MvcRequester.on(mockMvc)
.to("test/hello") // URI without starting `/`
.get()
// Assert
.expectStatus(HttpStatus.OK)
.expectStatus(OK)
.returnAsPrimitive(String.class);

assertThat(result).isEqualTo("hello world");
}

@Test
void urlPrefixInFirstArg() throws Exception {
void urlPrefixInFirstArg() {
// Act
String result = MvcRequester.on(mockMvc)
.to("{url}/hello", "/test") // starting `/` in the first arg
.get()
// Assert
.expectStatus(HttpStatus.OK)
.expectStatus(OK)
.returnAsPrimitive(String.class);

assertThat(result).isEqualTo("hello world");
}

@Test
void withoutUrlPrefixInFirstArg() throws Exception {
void withoutUrlPrefixInFirstArg() {
// Act
String result = MvcRequester.on(mockMvc)
.to("{url}/hello", "test")
.get()
// Assert
.expectStatus(HttpStatus.OK)
.expectStatus(OK)
.returnAsPrimitive(String.class);

assertThat(result).isEqualTo("hello world");
}

@Test
void returnPlainResponse() {
// Arrange
byte[] expected = "hello world".getBytes();
// Act
MockHttpServletResponse response = MvcRequester.on(mockMvc)
.to("/test/hello")
.get()
.returnResponse();
// Assert
assertThat(response.getStatus()).isEqualTo(OK.value());
byte[] result = response.getContentAsByteArray();
assertThat(Arrays.equals(expected, result)).isTrue();
}


@Configuration
@EnableWebMvc
static class WebConfig implements WebMvcConfigurer {
Expand Down

0 comments on commit 8e76489

Please sign in to comment.