Esempio n. 1
0
 public MockResponse handle(RecordedRequest request, String root) {
   String path = request.getPath();
   int index = path.indexOf("?");
   if (index > -1) {
     String queryString = path.substring(index + 1);
     path = path.substring(0, index);
     Map<String, String[]> paramMap = HttpUtil.getParamsMap(queryString, Util.UTF_8.name());
     System.out.println("Params:" + JSON.toJSONString(paramMap));
     String[] types = paramMap.get("type");
     if (types != null) {
       for (String type : types) {
         if ("json".equals(type.toLowerCase())) {
           return responseJson(root, path, paramMap);
         }
       }
     }
   }
   try {
     if (!path.startsWith("/") || path.contains("..")) throw new FileNotFoundException();
     path = URLDecoder.decode(path, Util.UTF_8.name());
     File file = new File(root + path);
     return file.isDirectory() ? directoryToResponse(path, file) : fileToResponse(path, file);
   } catch (FileNotFoundException e) {
     return new MockResponse()
         .setStatus("HTTP/1.1 404")
         .addHeader("content-type: text/plain; charset=utf-8")
         .setBody("NOT FOUND: " + path);
   } catch (IOException e) {
     return new MockResponse()
         .setStatus("HTTP/1.1 500")
         .addHeader("content-type: text/plain; charset=utf-8")
         .setBody("SERVER ERROR: " + e);
   }
 }
Esempio n. 2
0
 public void run() throws IOException {
   MockWebServer server = new MockWebServer();
   //        server.useHttps(sslContext.getSocketFactory(), false);
   server.setDispatcher(this);
   InetAddress address = HttpUtil.getHostAddress();
   server.start(address, port);
   System.out.println(
       String.format("Started server for: http://%s:%d/", address.getHostAddress(), port));
 }