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.springframework.extensions.webscripts.TestWebScriptServer.GetRequest;
26  import org.springframework.extensions.webscripts.TestWebScriptServer.Request;
27  import org.springframework.extensions.webscripts.TestWebScriptServer.Response;
28  
29  /**
30   * Unit test to test Web Script API
31   * 
32   * @author davidc
33   */
34  public class WebScriptExceptionTest extends TestCase
35  {
36      private static final String URL_EXCEPTION = "/test/exception?a=1";
37  
38      private static final TestWebScriptServer TEST_SERVER = TestWebScriptServer.getTestServer();
39  
40      /**
41       * Ensure that, for a non request type specific .js script, the request body
42       * is available as requestbody.
43       * 
44       * @throws Exception
45       */
46      public void testScriptStatusTemplate() throws Exception
47      {
48          String res = "Failed /alfresco/service/test/exception - args 1";
49          sendRequest(new GetRequest(URL_EXCEPTION), 500, res);
50      }
51  
52      /**
53       * @param req
54       * @param expectedStatus
55       * @param expectedResponse
56       * @return
57       * @throws IOException
58       */
59      private Response sendRequest(Request req, int expectedStatus, String expectedResponse) throws IOException
60      {
61          System.out.println();
62          System.out.println("* Request: " + req.getMethod() + " " + req.getFullUri() + (req.getBody() == null ? "" : "\n" + req.getBody()));
63  
64          Response res = TEST_SERVER.submitRequest(req);
65  
66          System.out.println();
67          System.out.println("* Response: " + res.getStatus() + " " + req.getMethod() + " " + req.getFullUri() + "\n" + res.getContentAsString());
68          if (expectedStatus > 0)
69          {
70              assertEquals("Unexpected status code", expectedStatus, res.getStatus());
71          }
72          if (expectedResponse != null)
73          {
74              assertEquals("Unexpected response", expectedResponse, res.getContentAsString());
75          }
76          return res;
77      }
78  }