1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.transport.http;
18  
19  import java.io.IOException;
20  
21  import org.springframework.beans.factory.annotation.Autowired;
22  import org.springframework.core.io.ClassPathResource;
23  import org.springframework.core.io.Resource;
24  import org.springframework.test.context.ContextConfiguration;
25  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
26  import org.springframework.ws.transport.TransportConstants;
27  
28  import org.apache.commons.httpclient.HttpClient;
29  import org.apache.commons.httpclient.methods.GetMethod;
30  import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
31  import org.apache.commons.httpclient.methods.PostMethod;
32  import org.junit.Before;
33  import org.junit.Test;
34  import org.junit.runner.RunWith;
35  
36  import static org.junit.Assert.assertEquals;
37  import static org.junit.Assert.assertTrue;
38  
39  @RunWith(SpringJUnit4ClassRunner.class)
40  @ContextConfiguration("httpserver-applicationContext.xml")
41  public class WebServiceHttpHandlerIntegrationTest {
42  
43      private HttpClient client;
44  
45      @Autowired
46      private int port;
47  
48      private String url;
49  
50      @Before
51      public void createHttpClient() throws Exception {
52          client = new HttpClient();
53          url = "http://localhost:" + port + "/service";
54      }
55  
56      @Test
57      public void testInvalidMethod() throws IOException {
58          GetMethod getMethod = new GetMethod(url);
59          client.executeMethod(getMethod);
60          assertEquals("Invalid Response Code", HttpTransportConstants.STATUS_METHOD_NOT_ALLOWED,
61                  getMethod.getStatusCode());
62          assertEquals("Response retrieved", 0, getMethod.getResponseContentLength());
63      }
64  
65      @Test
66      public void testNoResponse() throws IOException {
67          PostMethod postMethod = new PostMethod(url);
68          postMethod.addRequestHeader(HttpTransportConstants.HEADER_CONTENT_TYPE, "text/xml");
69          postMethod.addRequestHeader(TransportConstants.HEADER_SOAP_ACTION,
70                  "http://springframework.org/spring-ws/NoResponse");
71          Resource soapRequest = new ClassPathResource("soapRequest.xml", WebServiceHttpHandlerIntegrationTest.class);
72          postMethod.setRequestEntity(new InputStreamRequestEntity(soapRequest.getInputStream()));
73          client.executeMethod(postMethod);
74          assertEquals("Invalid Response Code", HttpTransportConstants.STATUS_ACCEPTED, postMethod.getStatusCode());
75          assertEquals("Response retrieved", 0, postMethod.getResponseContentLength());
76      }
77  
78      @Test
79      public void testResponse() throws IOException {
80          PostMethod postMethod = new PostMethod(url);
81          postMethod.addRequestHeader(HttpTransportConstants.HEADER_CONTENT_TYPE, "text/xml");
82          postMethod.addRequestHeader(TransportConstants.HEADER_SOAP_ACTION,
83                  "http://springframework.org/spring-ws/Response");
84          Resource soapRequest = new ClassPathResource("soapRequest.xml", WebServiceHttpHandlerIntegrationTest.class);
85          postMethod.setRequestEntity(new InputStreamRequestEntity(soapRequest.getInputStream()));
86          client.executeMethod(postMethod);
87          assertEquals("Invalid Response Code", HttpTransportConstants.STATUS_OK, postMethod.getStatusCode());
88          assertTrue("No Response retrieved", postMethod.getResponseContentLength() > 0);
89      }
90  
91      @Test
92      public void testNoEndpoint() throws IOException {
93          PostMethod postMethod = new PostMethod(url);
94          postMethod.addRequestHeader(HttpTransportConstants.HEADER_CONTENT_TYPE, "text/xml");
95          postMethod.addRequestHeader(TransportConstants.HEADER_SOAP_ACTION,
96                  "http://springframework.org/spring-ws/NoEndpoint");
97          Resource soapRequest = new ClassPathResource("soapRequest.xml", WebServiceHttpHandlerIntegrationTest.class);
98          postMethod.setRequestEntity(new InputStreamRequestEntity(soapRequest.getInputStream()));
99          client.executeMethod(postMethod);
100         assertEquals("Invalid Response Code", HttpTransportConstants.STATUS_NOT_FOUND, postMethod.getStatusCode());
101         assertEquals("Response retrieved", 0, postMethod.getResponseContentLength());
102     }
103 
104     @Test
105     public void testFault() throws IOException {
106         PostMethod postMethod = new PostMethod(url);
107         postMethod.addRequestHeader(HttpTransportConstants.HEADER_CONTENT_TYPE, "text/xml");
108         postMethod
109                 .addRequestHeader(TransportConstants.HEADER_SOAP_ACTION, "http://springframework.org/spring-ws/Fault");
110         Resource soapRequest = new ClassPathResource("soapRequest.xml", WebServiceHttpHandlerIntegrationTest.class);
111         postMethod.setRequestEntity(new InputStreamRequestEntity(soapRequest.getInputStream()));
112         client.executeMethod(postMethod);
113         assertEquals("Invalid Response Code", HttpTransportConstants.STATUS_INTERNAL_SERVER_ERROR,
114                 postMethod.getStatusCode());
115         assertTrue("No Response retrieved", postMethod.getResponseContentLength() > 0);
116     }
117 
118 }