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

Issues/56 #60

Merged
merged 4 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.stormeye.network;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMethod;

/**
* Marker Interface for CORS configuration
*/
@CrossOrigin(origins = {"audit.stormeye2000.com", "store.stormeye2000.com"})
@CrossOrigin(origins = {"audit.stormeye2000.com", "store.stormeye2000.com"}, methods = {RequestMethod.GET})
stormeye2000 marked this conversation as resolved.
Show resolved Hide resolved
public interface CrossOriginConfig {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.stormeye.network;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.WebApplicationContext;

import java.util.Collection;

@SpringBootTest
@TestPropertySource(locations = {"classpath:application-test.yml"})
public class CorsConfigTestFailure {

@Autowired
private WebApplicationContext context;

public MockMvc mockMvc;
stormeye2000 marked this conversation as resolved.
Show resolved Hide resolved


@BeforeEach
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
this.mockMvc.getDispatcherServlet().setThrowExceptionIfNoHandlerFound(true);
}

@Test
void testCorsConfig() throws Exception {

final MvcResult result = this.mockMvc
.perform(get("/test-cors"))
.andExpect(status().isOk()).andDo(print()).andReturn();

assertThat(result.getResponse().getContentAsString(), is("no cors"));

final Collection<String> headerNames = result.getResponse().getHeaderNames();

assertThat(headerNames, not(hasItem("Vary")));

}

@SpringBootConfiguration
@Controller
static class SpringApp {

public static void main(String[] args) {
SpringApplication.run(SpringApp.class, args);
}

@RequestMapping(value = {"test-cors"}, method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody String testCors() {
return "no cors";
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.stormeye.network;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.WebApplicationContext;

import java.util.Collection;
import java.util.List;

@SpringBootTest
@TestPropertySource(locations = {"classpath:application-test.yml"})
public class CorsConfigTestSuccess {

@Autowired
private WebApplicationContext context;

public MockMvc mockMvc;
stormeye2000 marked this conversation as resolved.
Show resolved Hide resolved


@BeforeEach
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
this.mockMvc.getDispatcherServlet().setThrowExceptionIfNoHandlerFound(true);
}

@Test
void testCorsConfig() throws Exception {

final MvcResult result = this.mockMvc
.perform(get("/test-cors"))
.andExpect(status().isOk()).andDo(print()).andReturn();

assertThat(result.getResponse().getContentAsString(), is("yes cors"));

final Collection<String> headerNames = result.getResponse().getHeaderNames();
assertThat(headerNames, hasItem("Vary"));

final List<Object> headers = result.getResponse().getHeaderValues("Vary");
assertThat(headers, hasItem("Origin"));
assertThat(headers, hasItem("Access-Control-Request-Method"));
assertThat(headers, hasItem("Access-Control-Request-Headers"));

}


@SpringBootConfiguration
@Controller
static class SpringApp implements CrossOriginConfig {

public static void main(String[] args) {
SpringApplication.run(SpringApp.class, args);
}

@RequestMapping(value = {"test-cors"}, method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody String testCors() {
return "yes cors";
}

}

}