1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http.nio.protocol;
29
30 import org.apache.http.HttpRequest;
31 import org.apache.http.HttpResponse;
32 import org.apache.http.HttpVersion;
33 import org.apache.http.message.BasicRequestLine;
34 import org.apache.http.protocol.BasicHttpContext;
35 import org.apache.http.protocol.HttpContext;
36 import org.apache.http.protocol.HttpRequestHandler;
37 import org.junit.After;
38 import org.junit.Assert;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.Matchers;
42 import org.mockito.Mockito;
43
44 public class TestBasicAsyncRequestHandler {
45
46 private HttpRequestHandler requestHandler;
47 private BasicAsyncRequestHandler asyncRequestHandler;
48 private HttpContext context;
49 private HttpRequest request;
50 private HttpResponse response;
51 private HttpAsyncExchange httpexchange;
52
53 @Before
54 public void setUp() throws Exception {
55 this.requestHandler = Mockito.mock(HttpRequestHandler.class);
56 this.asyncRequestHandler = new BasicAsyncRequestHandler(this.requestHandler);
57 this.context = new BasicHttpContext();
58 this.request = Mockito.mock(HttpRequest.class);
59 this.response = Mockito.mock(HttpResponse.class);
60 this.httpexchange = Mockito.mock(HttpAsyncExchange.class);
61 Mockito.when(this.httpexchange.getRequest()).thenReturn(this.request);
62 Mockito.when(this.httpexchange.getResponse()).thenReturn(this.response);
63 }
64
65 @After
66 public void tearDown() throws Exception {
67 }
68
69
70 @Test
71 public void testInvalidConstruction() throws Exception {
72 try {
73 new BasicAsyncRequestHandler(null);
74 Assert.fail("IllegalArgumentException expected");
75 } catch (final IllegalArgumentException ex) {
76 }
77 }
78
79 @Test
80 public void testProcessRequest() throws Exception {
81 final HttpAsyncRequestConsumer<HttpRequest> requestConsumer = this.asyncRequestHandler.processRequest(
82 this.request, this.context);
83 Assert.assertTrue(requestConsumer instanceof BasicAsyncRequestConsumer);
84 }
85
86 @Test
87 public void testHandleRequest() throws Exception {
88 Mockito.when(this.request.getRequestLine()).thenReturn(new BasicRequestLine("GET", "/", HttpVersion.HTTP_1_0));
89
90 this.asyncRequestHandler.handle(this.request, this.httpexchange, this.context);
91
92 Mockito.verify(this.requestHandler).handle(
93 Matchers.eq(this.request), Matchers.eq(this.response), Matchers.eq(this.context));
94 Mockito.verify(this.httpexchange).submitResponse();
95 }
96
97 }