Skip to content
Newer
Older
100644 67 lines (43 sloc) 3.03 KB
fb9e790 @nicokruger Added readme.
authored May 17, 2012
1 cannedhttp
2 ----------
3
4 Small, self-contained mock webserver for unit testing webservices over http.
5 The basic principle is simple - testing webservices requires having an endpoint to connect to, and being able to control what that endpoint returns. It is also beneficial if we could somehow inspect what we push to the endpoint as well.
6
7 The most basic usage of cannedhttp is to simply place specially formatted files in the classpath, under a certain package. These file are named for the URL they should represent, and contain data for the HTTP response code, the HTTP headers and the content of the body for the response. They are also parameterised according to the HTTP verb that is used.
8
9 So, for example, a file called users.GET inside the directory will be used when serving /users with a GET HTTP verb, and users.POST when POST verb is used. Similairly, placing a file /users/user/eddie.GET will serve as the canned response for the URL /users/user/eddie through the GET verb again. Files/directories are always for the URL they represent.
10
11 So, for example, a file structure like follows:
12
13 [f] /canned.GET
14 [f] /canned.POST
15 [d] /canned
16 [f] /canned/users.GET
17 [f] /canned/users.POST
18 [d] /canned/users
19 [f] /canned/users/eddie.GET
20 [f] /canned/users/eddie.POST
21
22 Will support all the following urls: /, /users, /users/eddie for both GET and POST verbs.
23
24 The structure of a file is simple:
25
26 <http response code>
27 <headers as key: value>
28 <blank line>
29 <content>
30
31 For example,
32
33 301
34 Content-type: text/html
35 X-Another-Header: Value
36
37 This is a 301
38
39 In addition to this, cannedhttp also supports a router API, allowing you to intercept HTTP requests and inspect the request content, and/or produce a completely custom response. This can be used to ensure that the initial HTTP request is formatted correctly, or the correct headers are set, etc. Here is an example of usage for checking for the presence of headers:
40
41
42
43 public void testHeaders() throws Exception {
44
45 router.on("/testHeaders").put(new F<Request, Response> () {
46 @Override
47 public Response f(Request a) {
48 System.out.println(a.headers.keySet());
49 Assert.assertEquals("application/json; charset=UTF8", a.headers.get("Content-Type"));
50 Assert.assertEquals("MyUserAgent", a.headers.get("User-Agent"));
51 Assert.assertTrue("Check authentication is http basic auth", a.headers.get("Authorization").contains("Basic "));
52 return Response.custom(200, Maps.<String,String>newHashMap(), "<html>Custom response content</html>");
53
54 });
2df3ad9 @nicokruger Readme updates.
authored May 17, 2012
55
56 httpGet("http://localhost:8080/testHeaders");
57
fb9e790 @nicokruger Added readme.
authored May 17, 2012
58 }
59
60
88f9705 @nicokruger Updated readme.
authored May 17, 2012
61 Advanced
62 --------
fb9e790 @nicokruger Added readme.
authored May 17, 2012
63
64 In more advanced usage scenarios, it could even be used to simulate a part of the webservice you are testing against, by returning different responses depending on request variables and so forth.
65
88f9705 @nicokruger Updated readme.
authored May 17, 2012
66 In theory, it could actually be used as a very primitive Java HTTP router, although this use is not recommended and not the intent of the project.
Something went wrong with that request. Please try again.