use of com.arbindo.mimock.entities.HttpMethod in project mimock by arbindo.
the class GenericMockRequestService method serveMockRequest.
public DomainModelForMock serveMockRequest(GenericRequestModel request) throws MatchingMockNotFoundException, IOException {
log.log(Level.INFO, "Fetching matching mock from the DB");
String route = request.getRoute();
HttpMethod httpMethod = httpMethod(request.getHttpMethod());
String queryParam = request.getQueryParam();
Optional<Mock> resultFromDB = getResultFromDB(route, httpMethod, queryParam);
if (resultFromDB.isEmpty()) {
log.log(Level.ERROR, "No matching rows returned from DB");
String errorMessage = "Matching mock does not exist";
throw new MatchingMockNotFoundException(errorMessage);
}
log.log(Level.INFO, "Returning matching mock");
Mock matchingMock = resultFromDB.get();
if (shouldValidateRequestHeaders(matchingMock, request)) {
validateRequestHeaders(request, matchingMock);
}
if (shouldValidateRequestBody(matchingMock, request)) {
validateRequestBody(matchingMock.getRequestBodiesForMock().getRequestBody(), request.getRequestBody());
}
return domainModelMapper.mappedModel(matchingMock);
}
use of com.arbindo.mimock.entities.HttpMethod in project mimock by arbindo.
the class HttpMethodsRepositoryTest method shouldReturnHttpMethodForValidMethod.
@ParameterizedTest
@ValueSource(strings = { "GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH" })
void shouldReturnHttpMethodForValidMethod(String method) {
// Act
HttpMethod httpMethod = httpMethodsRepository.findByMethod(method);
// Assert
assertNotNull(httpMethod);
assertEquals(method, httpMethod.getMethod());
}
use of com.arbindo.mimock.entities.HttpMethod in project mimock by arbindo.
the class HttpMethodsRepositoryTest method shouldReturnNullForInvalidMethodForSqlInjectionStrings.
@ParameterizedTest
@ValueSource(strings = { "get OR 1=1", "GET; DROP TABLE mocks;" })
void shouldReturnNullForInvalidMethodForSqlInjectionStrings(String method) {
// Act
HttpMethod httpMethod = httpMethodsRepository.findByMethod(method);
// Assert
assertNull(httpMethod);
}
use of com.arbindo.mimock.entities.HttpMethod in project mimock by arbindo.
the class HttpMethodsRepositoryTest method shouldReturnNullForInvalidMethodWhenEmptyOrNull.
@ParameterizedTest
@EmptySource
@NullSource
void shouldReturnNullForInvalidMethodWhenEmptyOrNull(String method) {
// Act
HttpMethod httpMethod = httpMethodsRepository.findByMethod(method);
// Assert
assertNull(httpMethod);
}
use of com.arbindo.mimock.entities.HttpMethod in project mimock by arbindo.
the class GetStaticRecordsControllerTest method listAllSupportedHttpMethods.
@Test
void listAllSupportedHttpMethods() throws Exception {
String route = UrlConfig.STATIC_RECORDS_PATH + UrlConfig.HTTP_METHODS_STATIC_RECORDS;
List<HttpMethod> httpMethodsFromDB = new ArrayList<>();
HttpMethod getMethod = HttpMethod.builder().method("GET").build();
HttpMethod postMethod = HttpMethod.builder().method("POST").build();
httpMethodsFromDB.add(getMethod);
httpMethodsFromDB.add(postMethod);
lenient().when(mockGetStaticRecordsService.listAllSupportedHttpMethods()).thenReturn(httpMethodsFromDB);
MvcResult result = mockMvc.perform(get(route).contentType(MediaType.APPLICATION_JSON_VALUE)).andExpect(status().isOk()).andReturn();
String response = result.getResponse().getContentAsString();
assertNotNull(response);
ObjectMapper objectMapper = new ObjectMapper();
List<HttpMethod> httpMethods = Arrays.asList(objectMapper.readValue(response, HttpMethod[].class));
assertNotNull(httpMethods);
assertFalse(httpMethods.isEmpty());
assertEquals("GET", httpMethods.get(0).getMethod());
assertEquals("POST", httpMethods.get(1).getMethod());
}
Aggregations