1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.webscripts;
20
21 import java.io.IOException;
22
23 import junit.framework.TestCase;
24
25 import org.json.JSONObject;
26 import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest;
27 import org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest;
28 import org.springframework.extensions.webscripts.TestWebScriptServer.Request;
29 import org.springframework.extensions.webscripts.TestWebScriptServer.Response;
30
31
32
33
34
35
36
37 public class WebScriptFormatReaderTest extends TestCase
38 {
39 private static final String URL_REQUESTBODY = "/test/requestbody";
40 private static final String URL_JSONECHO = "/test/jsonecho";
41 private static final String URL_ENCODEDPOST = "/test/encodedpost";
42 private static final String URL_ATOMENTRY = "/test/atomentry";
43 private static final String URL_BOGUS = "/test/bogus";
44 private static final TestWebScriptServer TEST_SERVER = TestWebScriptServer.getTestServer();
45
46
47
48
49
50
51
52 public void testRequestBody() throws Exception
53 {
54 String requestBody = "<html><head>Expected Result</head><body>Hello World</body></html>";
55 sendRequest(new PutRequest(URL_REQUESTBODY, requestBody, "text/html"), 200, requestBody);
56 }
57
58
59
60
61
62
63
64 public void testJson() throws Exception
65 {
66 JSONObject json = new JSONObject();
67 json.put("company", "Alfresco Software Inc.");
68 json.put("building", "Park House");
69 json.put("street", "Park Street");
70 json.put("town", "Maidenhead");
71
72 String postCode = "SL6 1SL";
73 json.put("postCode", postCode);
74 json.put("country", "United Kingdom");
75 json.put("year", 2008);
76 json.put("valid", true);
77
78 String requestBody = json.toString();
79 sendRequest(new PostRequest(URL_JSONECHO, requestBody, "application/json; charset=UTF-8"), 200, postCode);
80 }
81
82
83
84
85
86 public void testXWwwFormUrlEncoded() throws Exception
87 {
88
89 String requestBody = "param1=a¶m2=Hello+World";
90 String expectedResponse = "<html><body>a<br/>Hello World</body></html>";
91 sendRequest(new PostRequest(URL_ENCODEDPOST, requestBody,
92 "application/x-www-form-urlencoded; charset=UTF-8"), 200, expectedResponse);
93 }
94
95
96
97
98
99
100
101
102 public void testAtomEntry() throws Exception
103 {
104 String entryTitle = "Test Atom Entry";
105 String requestBody = "<entry xmlns=\"http://www.w3.org/2005/Atom\">" + "<title>" + entryTitle + "</title>"
106 + "</entry>";
107 sendRequest(new PostRequest(URL_ATOMENTRY, requestBody, "application/atom+xml;type=entry"), 200, entryTitle);
108 }
109
110
111
112
113
114
115
116
117 public void testBogus() throws Exception
118 {
119 String requestBody = "I've got a lovely bunch of coconuts";
120 sendRequest(new PostRequest(URL_BOGUS, requestBody, "application/bogus"), 500, null);
121 }
122
123
124
125
126
127
128
129
130 private Response sendRequest(Request req, int expectedStatus, String expectedResponse) throws IOException
131 {
132 System.out.println();
133 System.out.println("* Request: " + req.getMethod() + " " + req.getFullUri()
134 + (req.getBody() == null ? "" : "\n" + req.getBody()));
135
136 Response res = TEST_SERVER.submitRequest(req);
137
138 System.out.println();
139 System.out.println("* Response: " + res.getStatus() + " " + req.getMethod() + " " + req.getFullUri() + "\n"
140 + res.getContentAsString());
141 if (expectedStatus > 0)
142 {
143 assertEquals("Unexpected status code", expectedStatus, res.getStatus());
144 }
145 if (expectedResponse != null)
146 {
147 assertEquals("Unexpected response", expectedResponse, res.getContentAsString());
148 }
149 return res;
150 }
151 }