Skip to content

Commit

Permalink
code cleanup after #931
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Oct 21, 2019
1 parent 24e3ae8 commit 9d27729
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 67 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,6 @@ And if you run into class-loading conflicts, for example if an older version of

If you want to use [JUnit 5](#junit-5), use `karate-junit5` instead of `karate-junit4`.

> The [Karate UI](https://github.com/intuit/karate/wiki/Karate-UI) is no longer part of the core framework from 0.9.3 onwards, and is an optional dependency called `karate-ui`.
## Gradle
Alternatively for [Gradle](https://gradle.org) you need these two entries:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,54 +22,36 @@ public ErrorResponse(int code, String path, String method, String message) {
this.message = message;
}

/**
* @return the code
*/
public int getCode() {
return code;}
public int getCode() {
return code;
}

/**
* @param code the code to set
*/
public void setCode(int code) {
this.code = code;}
public void setCode(int code) {
this.code = code;
}

/**
* @return the path
*/
public String getPath() {
return path;}
public String getPath() {
return path;
}

/**
* @param path the path to set
*/
public void setPath(String path) {
this.path = path;}
public void setPath(String path) {
this.path = path;
}

/**
* @return the method
*/
public String getMethod() {
return method;}
public String getMethod() {
return method;
}

/**
* @param method the method to set
*/
public void setMethod(String method) {
this.method = method;}
public void setMethod(String method) {
this.method = method;
}

/**
* @return the message
*/
public String getMessage() {
return message;}
public String getMessage() {
return message;
}

/**
* @param message the message to set
*/
public void setMessage(String message) {
this.message = message;}
public void setMessage(String message) {
this.message = message;
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

/**
/**
* Adding these properties will make the following code active:
* spring.mvc.throw-exception-if-no-handler-found=true
* spring.resources.add-mappings=false
Expand All @@ -35,4 +35,5 @@ protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundExc
ErrorResponse errorResponse = new ErrorResponse(status.value(), uriPath, method, message);
return new ResponseEntity<>(errorResponse, status);
}

}
1 change: 0 additions & 1 deletion karate-mock-servlet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ Use the test configuration for this `karate-mock-servlet` project as a reference
## Limitations
Most teams would not run into these, but if you do, please [consider contributing](https://github.com/intuit/karate/projects/3#card-22529274) !

* Servlet filters that may be "default" in "real" spring / boot apps etc will be missing, for e.g. encoding and error handling. Currently we lack a way to add custom filters to the "fake" servlet.
* File Upload is not supported.
* Other similar edge-cases (such as redirects) are not supported.

Expand Down
4 changes: 1 addition & 3 deletions karate-mock-servlet/src/test/java/demo/MockDemoConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,10 @@ public SoapController soapController() {
public EchoController echoController() {
return new EchoController();
}

// Global Exception Handler ...

@Bean
public GlobalExceptionHandler globalExceptionHandler() {
return new GlobalExceptionHandler();
}


}
37 changes: 19 additions & 18 deletions karate-mock-servlet/src/test/java/demo/MockSpringMvcServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class MockSpringMvcServlet extends MockHttpClient {

private final Servlet servlet;
private final ServletContext servletContext;

public MockSpringMvcServlet(Servlet servlet, ServletContext servletContext) {
this.servlet = servlet;
this.servletContext = servletContext;
Expand All @@ -59,14 +59,14 @@ protected Servlet getServlet(HttpRequestBuilder request) {
protected ServletContext getServletContext() {
return servletContext;
}

private static final ServletContext SERVLET_CONTEXT = new MockServletContext();
private static final Servlet SERVLET;

static {
SERVLET = initServlet();
}

private static Servlet initServlet() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(MockDemoConfig.class);
Expand All @@ -80,27 +80,28 @@ private static Servlet initServlet() {
throw new RuntimeException(e);
}
return servlet;
}
}

/**
* Checks if servlet is Dispatcher servlet implementation and then fetches the WebMvcProperties
* from spring container and configure the dispatcher servlet.
* Checks if servlet is Dispatcher servlet implementation and then fetches
* the WebMvcProperties from spring container and configure the dispatcher
* servlet.
*
* @param servlet input servlet implementation
*/
private static void customize(Servlet servlet) {
if (servlet instanceof DispatcherServlet) {
DispatcherServlet dispatcherServlet = (DispatcherServlet) servlet;
WebMvcProperties mvcProperties =
dispatcherServlet.getWebApplicationContext().getBean(WebMvcProperties.class);
dispatcherServlet.setThrowExceptionIfNoHandlerFound(mvcProperties.isThrowExceptionIfNoHandlerFound());
dispatcherServlet.setDispatchOptionsRequest(mvcProperties.isDispatchOptionsRequest());
dispatcherServlet.setDispatchTraceRequest(mvcProperties.isDispatchTraceRequest());
}
if (servlet instanceof DispatcherServlet) {
DispatcherServlet dispatcherServlet = (DispatcherServlet) servlet;
WebMvcProperties mvcProperties
= dispatcherServlet.getWebApplicationContext().getBean(WebMvcProperties.class);
dispatcherServlet.setThrowExceptionIfNoHandlerFound(mvcProperties.isThrowExceptionIfNoHandlerFound());
dispatcherServlet.setDispatchOptionsRequest(mvcProperties.isDispatchOptionsRequest());
dispatcherServlet.setDispatchTraceRequest(mvcProperties.isDispatchTraceRequest());
}
}

public static MockSpringMvcServlet getMock() {
return new MockSpringMvcServlet(SERVLET, SERVLET_CONTEXT);
}

}

0 comments on commit 9d27729

Please sign in to comment.