1   /*
2    * Copyright 2002-2009 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.soap.soap12;
18  
19  import java.io.ByteArrayOutputStream;
20  
21  import junit.framework.Assert;
22  
23  import org.springframework.core.io.ByteArrayResource;
24  import org.springframework.core.io.ClassPathResource;
25  import org.springframework.core.io.InputStreamSource;
26  import org.springframework.core.io.Resource;
27  import org.springframework.ws.soap.AbstractSoapMessageTestCase;
28  import org.springframework.ws.soap.SoapBody;
29  import org.springframework.ws.soap.SoapVersion;
30  import org.springframework.ws.transport.MockTransportOutputStream;
31  import org.springframework.ws.transport.TransportConstants;
32  import org.springframework.xml.transform.StringSource;
33  
34  public abstract class AbstractSoap12MessageTestCase extends AbstractSoapMessageTestCase {
35  
36      public void testGetVersion() throws Exception {
37          Assert.assertEquals("Invalid SOAP version", SoapVersion.SOAP_12, soapMessage.getVersion());
38      }
39  
40      protected final Resource[] getSoapSchemas() {
41          return new Resource[]{new ClassPathResource("xml.xsd", AbstractSoap12MessageTestCase.class),
42                  new ClassPathResource("soap12.xsd", AbstractSoap12MessageTestCase.class)};
43      }
44  
45      public void testWriteToTransportOutputStream() throws Exception {
46          SoapBody body = soapMessage.getSoapBody();
47          String payload = "<payload xmlns='http://www.springframework.org' />";
48          transformer.transform(new StringSource(payload), body.getPayloadResult());
49  
50          final ByteArrayOutputStream bos = new ByteArrayOutputStream();
51          MockTransportOutputStream tos = new MockTransportOutputStream(bos);
52          String soapAction = "http://springframework.org/spring-ws/Action";
53          soapMessage.setSoapAction(soapAction);
54          soapMessage.writeTo(tos);
55          String result = bos.toString("UTF-8");
56          assertXMLEqual(
57                  "<Envelope xmlns='http://www.w3.org/2003/05/soap-envelope'><Body><payload xmlns='http://www.springframework.org' /></Body></Envelope>",
58                  result);
59          String contentType = (String) tos.getHeaders().get(TransportConstants.HEADER_CONTENT_TYPE);
60          assertTrue("Invalid Content-Type set", contentType.indexOf(SoapVersion.SOAP_12.getContentType()) != -1);
61          assertNull(TransportConstants.HEADER_SOAP_ACTION + " header must not be found",
62                  tos.getHeaders().get(TransportConstants.HEADER_SOAP_ACTION));
63          assertTrue("Invalid Content-Type set", contentType.indexOf(soapAction) != -1);
64          String resultAccept = (String) tos.getHeaders().get("Accept");
65          assertNotNull("Invalid accept header", resultAccept);
66      }
67  
68      public void testWriteToTransportResponseAttachment() throws Exception {
69          InputStreamSource inputStreamSource = new ByteArrayResource("contents".getBytes("UTF-8"));
70          soapMessage.addAttachment("contentId", inputStreamSource, "text/plain");
71          ByteArrayOutputStream bos = new ByteArrayOutputStream();
72          MockTransportOutputStream tos = new MockTransportOutputStream(bos);
73          soapMessage.writeTo(tos);
74          String contentType = (String) tos.getHeaders().get("Content-Type");
75          assertTrue("Content-Type for attachment message does not contains multipart/related",
76                  contentType.indexOf("multipart/related") != -1);
77          assertTrue("Content-Type for attachment message does not contains type=\"application/soap+xml\"",
78                  contentType.indexOf("type=\"application/soap+xml\"") != -1);
79      }
80  
81  }