View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.client.integration;
28  
29  import java.io.IOException;
30  import java.net.Socket;
31  
32  import org.apache.http.HttpConnectionFactory;
33  import org.apache.http.HttpException;
34  import org.apache.http.HttpHost;
35  import org.apache.http.HttpRequest;
36  import org.apache.http.HttpResponse;
37  import org.apache.http.HttpStatus;
38  import org.apache.http.client.methods.CloseableHttpResponse;
39  import org.apache.http.client.methods.HttpGet;
40  import org.apache.http.entity.StringEntity;
41  import org.apache.http.impl.DefaultBHttpServerConnection;
42  import org.apache.http.localserver.LocalServerTestBase;
43  import org.apache.http.protocol.HttpContext;
44  import org.apache.http.protocol.HttpRequestHandler;
45  import org.apache.http.util.EntityUtils;
46  import org.junit.Assert;
47  import org.junit.Test;
48  
49  public class TestMalformedServerResponse extends LocalServerTestBase {
50  
51      static class BrokenServerConnection extends DefaultBHttpServerConnection {
52  
53          public BrokenServerConnection(final int bufferSize) {
54              super(bufferSize);
55          }
56  
57          @Override
58          public void sendResponseHeader(final HttpResponse response) throws HttpException, IOException {
59              super.sendResponseHeader(response);
60              if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
61                  response.setEntity(new StringEntity(
62                          "garbage\ngarbage\n" +
63                          "garbage\ngarbage\n" +
64                          "garbage\ngarbage\n" +
65                          "garbage\ngarbage\n" +
66                          "garbage\ngarbage\n" +
67                          "garbage\ngarbage\n" +
68                          "garbage\ngarbage\n" +
69                          "garbage\ngarbage\n" +
70                          "garbage\ngarbage\n"));
71                  sendResponseEntity(response);
72              }
73          }
74      }
75  
76      static class BrokenServerConnectionFactory implements HttpConnectionFactory<DefaultBHttpServerConnection> {
77  
78          @Override
79          public DefaultBHttpServerConnection createConnection(final Socket socket) throws IOException {
80              final BrokenServerConnection conn = new BrokenServerConnection(4096);
81              conn.bind(socket);
82              return conn;
83          }
84      }
85  
86      @Test
87      public void testNoContentResponseWithGarbage() throws Exception {
88          this.serverBootstrap.setConnectionFactory(new BrokenServerConnectionFactory());
89          this.serverBootstrap.registerHandler("/nostuff", new HttpRequestHandler() {
90  
91              @Override
92              public void handle(
93                      final HttpRequest request,
94                      final HttpResponse response,
95                      final HttpContext context) throws HttpException, IOException {
96                  response.setStatusCode(HttpStatus.SC_NO_CONTENT);
97              }
98  
99          });
100         this.serverBootstrap.registerHandler("/stuff", new HttpRequestHandler() {
101 
102             @Override
103             public void handle(
104                     final HttpRequest request,
105                     final HttpResponse response,
106                     final HttpContext context) throws HttpException, IOException {
107                 response.setStatusCode(HttpStatus.SC_OK);
108                 response.setEntity(new StringEntity("Some important stuff"));
109             }
110 
111         });
112 
113         final HttpHost target = start();
114         final HttpGet get1 = new HttpGet("/nostuff");
115         final CloseableHttpResponse response1 = this.httpclient.execute(target, get1);
116         try {
117             Assert.assertEquals(HttpStatus.SC_NO_CONTENT, response1.getStatusLine().getStatusCode());
118             EntityUtils.consume(response1.getEntity());
119         } finally {
120             response1.close();
121         }
122         final HttpGet get2 = new HttpGet("/stuff");
123         final CloseableHttpResponse response2 = this.httpclient.execute(target, get2);
124         try {
125             Assert.assertEquals(HttpStatus.SC_OK, response2.getStatusLine().getStatusCode());
126             EntityUtils.consume(response2.getEntity());
127         } finally {
128             response2.close();
129         }
130     }
131 
132 }