1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Unit test to test Web Script API
33   * 
34   * @author David Ward
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       * Ensure that, for a non request type specific .js script, the request body
48       * is available as requestbody.
49       * 
50       * @throws Exception
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       * Ensure that for a .json.js script and an application/json request, the
60       * json string is available as "json".
61       * 
62       * @throws Exception
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       * @throws Exception
85       */
86      public void testXWwwFormUrlEncoded() throws Exception
87      {
88          //FIXME URL-encoded post of forms data is not yet working.
89          String requestBody = "param1=a&param2=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       * Ensure that for a .atom.js script and an application/atom+xml;type=entry
97       * request (for which a less generalized atomentry format is registered) the
98       * entry variable is available as "entry".
99       * 
100      * @throws Exception
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      * Ensure that for a .bogus.js script and an application/bogus request, an
112      * error is returned because the bogus format is registered, but no
113      * FormatReader is registered.
114      * 
115      * @throws Exception
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      * @param req
125      * @param expectedStatus
126      * @param expectedResponse
127      * @return
128      * @throws IOException
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 }