Skip to content

Commit

Permalink
Merge pull request #12 from Dh3356/test/restinterceptor-test-#11
Browse files Browse the repository at this point in the history
Creating tests for RestInterceptor
  • Loading branch information
Dh3356 authored Dec 2, 2024
2 parents 724962a + 2e7c767 commit 41d46ff
Showing 1 changed file with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.restful_spring.rest_interceptor;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.http.HttpMethod.GET;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpMethod;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

class RestInterceptorTest {

private static final String FOO = "/foo";
private static final String BAR = "/bar";
private static final RestfulPattern REGISTRATION_PATTERN = RestfulPattern.of(FOO, GET);
private RestInterceptor interceptor;
private MockHttpServletRequest request;
private MockHttpServletResponse response;

@BeforeEach
void setUp() {
interceptor = new RestInterceptor() {
@Override
protected boolean doInternal(HttpServletRequest request, HttpServletResponse response, Object handler) {
return true;
}
};

request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
}

@Test
void preFlightRequest() {
// Given
request.setMethod(HttpMethod.OPTIONS.name());

// When
boolean result = interceptor.preHandle(request, response, new Object());

// Then
assertThat(result).isTrue();
}

@Test
void pathMatchingWithRestfulPattern() {
// Given
interceptor.restfulPatterns = List.of(REGISTRATION_PATTERN);

// When
request.setRequestURI(FOO);
request.setMethod(GET.name());
boolean result = interceptor.preHandle(request, response, new Object());

// Then
assertThat(result).isTrue();
}

@Test
void skipWithNonMatchingPath() {
// Given
interceptor.restfulPatterns = List.of(REGISTRATION_PATTERN);

// When
request.setRequestURI(BAR);
request.setMethod(GET.name());
boolean result = interceptor.preHandle(request, response, new Object());

// Then
assertThat(result).isTrue();
}

@Test
void shouldSkipWhenNoPatterns() {
// Given
interceptor.restfulPatterns = List.of();

// When
request.setRequestURI(FOO);
boolean result = interceptor.preHandle(request, response, new Object());

// Then
assertThat(result).isTrue();
}

@Test
void doInternalCalledForMatchingRequest() {
// Given
interceptor.restfulPatterns = List.of(REGISTRATION_PATTERN);

// When
request.setRequestURI(FOO);
request.setMethod(GET.name());

// Then
boolean result = interceptor.preHandle(request, response, new Object());
assertThat(result).isTrue();
}

@Test
void doInternalNotCalledForNonMatchingRequest() {
// Given
interceptor.restfulPatterns = List.of(REGISTRATION_PATTERN);

// When
request.setRequestURI(BAR);
request.setMethod(GET.name());

// Then
boolean result = interceptor.preHandle(request, response, new Object());
assertThat(result).isTrue();
}
}

0 comments on commit 41d46ff

Please sign in to comment.