blob: f752ac86dd02abe9a421753d45f4fa68d60b6ef7 (
plain) (
blame)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.test;
import com.yahoo.jdisc.Request;
import com.yahoo.jdisc.Response;
import com.yahoo.jdisc.application.BindingRepository;
import com.yahoo.jdisc.handler.AbstractRequestHandler;
import com.yahoo.jdisc.handler.ContentChannel;
import com.yahoo.jdisc.handler.ResponseDispatch;
import com.yahoo.jdisc.handler.ResponseHandler;
import com.yahoo.jdisc.http.HttpRequest;
import com.yahoo.jdisc.http.filter.RequestFilter;
import com.yahoo.jdisc.http.filter.ResponseFilter;
import java.io.IOException;
import java.util.concurrent.Exchanger;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import static com.yahoo.jdisc.http.test.ServerTestDriver.newFilterModule;
/**
* @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
*
* TODO: dead code?
*/
public class FilterTestDriver {
private final ServerTestDriver driver;
private final MyRequestHandler requestHandler;
private FilterTestDriver(ServerTestDriver driver, MyRequestHandler requestHandler) {
this.driver = driver;
this.requestHandler = requestHandler;
}
public boolean close() throws IOException {
return driver.close();
}
public HttpRequest filterRequest(String request) throws IOException, TimeoutException, InterruptedException {
driver.client().writeRequest(request);
return (HttpRequest)requestHandler.exchanger.exchange(null, 60, TimeUnit.SECONDS);
}
public static FilterTestDriver newInstance(final BindingRepository<RequestFilter> requestFilters,
final BindingRepository<ResponseFilter> responseFilters)
throws IOException {
MyRequestHandler handler = new MyRequestHandler();
return new FilterTestDriver(ServerTestDriver.newInstance(handler,
newFilterModule(requestFilters, responseFilters)),
handler);
}
private static class MyRequestHandler extends AbstractRequestHandler {
final Exchanger<Request> exchanger = new Exchanger<>();
@Override
public ContentChannel handleRequest(Request request, ResponseHandler handler) {
ResponseDispatch.newInstance(Response.Status.OK).dispatch(handler);
try {
exchanger.exchange(request);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
}
}
|