Use Embedded Jetty as Mock Server

When writing an interface automation test code, the test environment crashed, but I need to continue debugging. What should I do?

Decided to write a mock server to return the interface response, using embedded jetty, and logback using logback.

Requirements
1. When using the GET method to access http://localhost:8080/version/1.0/versions/latest, the following JSON string will be returned:
{“code”: “0000”,”content”:{“newVersion”:0},”message”:”success”}

2, when using the POST method to visit http://localhost:8080/auth/1.0/ When tokens, the following JSON string will be returned:
{“code”:”0000″,”content”:{…},”message”:”success”}

First of all, in maven Add the following dependencies in:


org.eclipse.jetty
jetty-server
9.4.0.RC3


ch.qos.logback
logback-classic
1.2.3

Then the mock server class:

package com.szsharelink.test.mock;import org.eclipse.jetty.server .Server;import org.eclipse.jetty.server.handler.ContextHandler;import org.eclipse.jetty .server.handler .ContextHandlerCollection;/** * Created by Liliangxi on 2017/8/12. */public class EmbeddedJettyServer { private Server server; private int port = 8888; public EmbeddedJettyServer( int port) {this.port = port;} public void start() throws Exception{ server = new Server(port); ContextHandler contextVersion = new ContextHandler( "/version/1.0/versions/latest"); contextVersion.setHandler(new GetVersionHandler()); ContextHandler contextToken = new ContextHandler("/auth/1.0/tokens"); contextToken.setAllowNullPathInfo(true); contextToken.setHandler(new PostTokenHandler()); ContextHandlerCollection contexts = new ContextHandlerCollection(); contexts.addHandler(contextVersion); contexts.addHandler(contextToken); server.setHandler(contexts); server.start(); // server.join(); // This is not allowed, otherwise the subsequent client calls will not proceed} public  void stop(){ try{ s erver.stop(); }catch (Exception e){} }}

The Handler code is as follows:

import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;/** * Created by Liliangxi on 2017/8/12. */public class GetVersionHandler extends AbstractHandler{ @Override public void handle(String s, Request request, HttpServletRequest httpServletRequ est, HttpServletResponse httpServletResponse) throws IOException, ServletException {// Declare response encoding and types httpServletResponse.setContentType(< span class="hljs-string">"application/json; charset=utf-8"); // Declare response status code httpServletResponse.setStatus( HttpServletResponse.SC_OK); // Write back response // {"code":"0000","content":{ "newVersion":0},"message":"Success"} httpServletResponse.getWriter().println("{\"code\":\"0000\" ,\"content\":{\"newVersion\":0},\"message\":\"success\"}"); // Inform jetty that this request has now been handled request.setHandled(true); }}

Start like this in the BeforeClass function:

@Befor eClasspublic void before() throws Exception{ RestAssured.baseURI = "http://192.168.0.60"; RestAssured.port =  8080; RestAssured.registerParser("text/plain", Parser.JSON); EmbeddedJettyServer mockServer = new EmbeddedJettyServer(8080); mockServer.start(); // do other things ...}

Stop jetty server in the AfterClass function :

@AfterClasspublic void afterClass(){ mockServer.stop();}

There are several important points to note in the article:
1. contextToken.setAllowNullPathInfo(true);
Many people write programs to send POST messages to jetty, which will return a 302 redirect error.
There are two processing methods (in fact, there is only one), one is to add “/” at the end of the address of the request message, which is the request: http://IP:port/url/. One is the method used in the article.

2, server.join();
If you write join after start according to the online example, then the next client request will not be sent out.

3. How to avoid jetty from hitting a large number of logs when using logback?
Write this in logback.xml:

<logger < span class="hljs-attribute">name="org.eclipse.jetty" level="INFO" />

In this way, you can write various tests in @Test. Go to this embedded jetty The server sends a message and gets the response message set.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4581 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.