Skip to content

Commit

Permalink
[rewrite] #1281 focused commit to show changes to [karate-mock-servlet]
Browse files Browse the repository at this point in the history
now it is simpler, using the http-client-factory, and avoiding js
  • Loading branch information
ptrthomas committed Nov 11, 2020
1 parent e7b1f05 commit 41fef71
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 609 deletions.

Large diffs are not rendered by default.

This file was deleted.

This file was deleted.

70 changes: 21 additions & 49 deletions karate-mock-servlet/src/test/java/demo/MockSpringMvcServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
*/
package demo;

import com.intuit.karate.http.HttpRequestBuilder;
import com.intuit.karate.mock.servlet.MockHttpClient;

import javax.servlet.Servlet;
import com.intuit.karate.runtime.ScenarioEngine;
import com.intuit.karate.server.HttpClient;
import com.intuit.karate.server.HttpClientFactory;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import org.slf4j.Logger;
Expand All @@ -42,67 +42,39 @@
*
* @author pthomas3
*/
public class MockSpringMvcServlet extends MockHttpClient {
public class MockSpringMvcServlet implements HttpClientFactory {

private static final Logger logger = LoggerFactory.getLogger(MockSpringMvcServlet.class);

private final Servlet servlet;
private final ServletContext servletContext;
private final DispatcherServlet servlet;

public MockSpringMvcServlet(Servlet servlet, ServletContext servletContext) {
this.servlet = servlet;
this.servletContext = servletContext;
}

@Override
protected Servlet getServlet(HttpRequestBuilder request) {
return servlet;
}

@Override
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() {
public MockSpringMvcServlet() {
servletContext = new MockServletContext();
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(MockDemoConfig.class);
context.setServletContext(SERVLET_CONTEXT);
DispatcherServlet servlet = new DispatcherServlet(context);
context.setServletContext(servletContext);
servlet = new DispatcherServlet(context);
ServletConfig servletConfig = new MockServletConfig();
try {
servlet.init(servletConfig);
customize(servlet);
// if you want things like error handling, encoding etc to work exactly as the "real" spring DispatcherServlet
// you may need to add filters and beans to make some tests pass
// this may not be worth it, so alternatively use tags to exclude those tests from your local mock-servlet based tests
// note that this code below e.g. the WebMvcProperties depends on spring-boot 1.5X
WebMvcProperties mvcProperties = servlet.getWebApplicationContext().getBean(WebMvcProperties.class);
servlet.setThrowExceptionIfNoHandlerFound(mvcProperties.isThrowExceptionIfNoHandlerFound());
servlet.setDispatchOptionsRequest(mvcProperties.isDispatchOptionsRequest());
servlet.setDispatchTraceRequest(mvcProperties.isDispatchTraceRequest());
} catch (Exception e) {
logger.error("init failed: {}", e.getMessage());
throw new RuntimeException(e);
}
return servlet;
}

// if you want things like error handling, encoding etc to work exactly as the "real" spring DispatcherServlet
// you may need to add filters and beans to make some tests pass
// this may not be worth it, so alternatively use tags to exclude those tests from your local mock-servlet based tests
// note that this code below e.g. the WebMvcProperties depends on spring-boot 1.5X
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());
}
}

public static MockSpringMvcServlet getMock() {
return new MockSpringMvcServlet(SERVLET, SERVLET_CONTEXT);
@Override
public HttpClient create(ScenarioEngine engine) {
return new MockHttpClient(engine, servlet, servletContext);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import com.intuit.karate.Runner;
import com.intuit.karate.Results;
import com.intuit.karate.KarateOptions;
import java.io.File;
import org.apache.commons.io.FileUtils;
import static org.junit.Assert.assertTrue;
Expand All @@ -35,18 +34,21 @@
*
* @author pthomas3
*/
@KarateOptions(tags = {"~@ignore", "~@mock-servlet-todo"})
public class MockSpringMvcServletTest {

@Test
public void testSpringBootDemo() throws Exception {
public void testSpringBootDemo() throws Exception {
File srcDir = new File("../karate-demo/src/test/java");
File destDir = new File("target/test-classes");
FileUtils.copyDirectory(srcDir, destDir,
FileUtils.copyDirectory(srcDir, destDir,
f -> !f.getName().equals("karate-config.js"), false); // don't over-write karate-config.js
System.setProperty("karate.env", "dev-mock-springmvc");
Results results = Runner.parallel(getClass(), 5);
assertTrue("there are scenario failures", results.getFailCount() == 0);
MockSpringMvcServlet factory = new MockSpringMvcServlet();
Results results = Runner.path("classpath:demo")
.tags("~@ignore", "~@mock-servlet-todo")
.karateEnv("dev-mock-springmvc")
.clientFactory(factory)
.parallel(1);
assertTrue(results.getErrorMessages(), results.getFailCount() == 0);
}

}
9 changes: 1 addition & 8 deletions karate-mock-servlet/src/test/java/karate-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@ function fn() {
var config = {
demoBaseUrl: 'http://localhost:8080'
};
if (karate.env == 'dev-mock') {
karate.configure('httpClientClass', 'mock.jersey.MockJerseyServlet');
} else if (karate.env == 'dev-mock-factory') {
var Factory = Java.type('mock.jersey.MockJerseyServletFactory');
karate.configure('httpClientInstance', Factory.getMock());
} else if (karate.env == 'dev-mock-springmvc') {
var Factory = Java.type('demo.MockSpringMvcServlet');
karate.configure('httpClientInstance', Factory.getMock());
if (karate.env == 'dev-mock-springmvc') {
var result = karate.callSingle('classpath:demo/headers/common-noheaders.feature', config);
config.authInfo = { authTime: result.time, authToken: result.token };
}
Expand Down
Loading

0 comments on commit 41fef71

Please sign in to comment.