本文整理汇总了Java中io.netty.handler.codec.http.HttpHeaders.add方法的典型用法代码示例。如果您正苦于以下问题:Java HttpHeaders.add方法的具体用法?Java HttpHeaders.add怎么用?Java HttpHeaders.add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.handler.codec.http.HttpHeaders
的用法示例。
在下文中一共展示了HttpHeaders.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testConvert
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Test
public void testConvert() throws IOException {
Response responseMock = mock(Response.class);
HttpHeaders headers = new DefaultHttpHeaders();
headers.add(TEST_KEY_A, TEST_VALUE_B);
headers.add(TEST_KEY_C, TEST_VALUE_D);
when(responseMock.getHeaders()).thenReturn(headers);
when(responseMock.getStatusCode()).thenReturn(STATUS_CODE);
when(responseMock.getStatusText()).thenReturn(STATUS_TEXT);
when(responseMock.getResponseBodyAsStream()).thenReturn(
new ByteArrayInputStream(ENCODED_BODY.getBytes(StandardCharsets.UTF_8))
);
BiFunction<Response, Request, HttpResponse> converter = new AsyncResponseConverter();
HttpResponse awsResponse = converter.apply(responseMock, null);
assertThat(awsResponse.getHeaders().get(TEST_KEY_A)).isEqualTo(TEST_VALUE_B);
assertThat(awsResponse.getHeaders().get(TEST_KEY_C)).isEqualTo(TEST_VALUE_D);
assertThat(awsResponse.getStatusCode()).isEqualTo(STATUS_CODE);
assertThat(awsResponse.getStatusText()).isEqualTo(STATUS_TEXT);
assertThat(new BufferedReader(new InputStreamReader(awsResponse.getContent())).readLine())
.isEqualTo(ENCODED_BODY);
}
示例2: test_validateSecureRequestForEndpoint_adds_security_context_to_request
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Test
public void test_validateSecureRequestForEndpoint_adds_security_context_to_request() {
final RequestInfo<Void> requestInfo = mock(RequestInfo.class);
when(requestInfo.getUri()).thenReturn("https://localhost");
final HttpHeaders httpHeaders = new DefaultHttpHeaders();
httpHeaders.add(CmsRequestSecurityValidator.HEADER_X_VAULT_TOKEN, vaultToken);
when(requestInfo.getHeaders()).thenReturn(httpHeaders);
final Map<String, String> meta = Maps.newHashMap();
meta.put(VaultAuthPrincipal.METADATA_KEY_IS_ADMIN, Boolean.TRUE.toString());
meta.put(VaultAuthPrincipal.METADATA_KEY_USERNAME, "username");
meta.put(VaultAuthPrincipal.METADATA_KEY_GROUPS, "group1,group2");
final VaultClientTokenResponse clientTokenResponse = new VaultClientTokenResponse()
.setId(vaultToken)
.setMeta(meta);
when(vaultAdminClient.lookupToken(vaultToken)).thenReturn(clientTokenResponse);
subject.validateSecureRequestForEndpoint(requestInfo, securedEndpoint);
verify(requestInfo).addRequestAttribute(eq(SECURITY_CONTEXT_ATTR_KEY), any(SecurityContext.class));
}
示例3: execute_throws_api_error_when_bad_auth_header
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Test
public void execute_throws_api_error_when_bad_auth_header() {
final RequestInfo<Void> requestInfo = mock(RequestInfo.class);
final HttpHeaders httpHeaders = new DefaultHttpHeaders();
httpHeaders.add(HttpHeaders.Names.AUTHORIZATION, invalidAuthorizationHeader);
when(requestInfo.getHeaders()).thenReturn(httpHeaders);
try {
final CompletableFuture<ResponseInfo<AuthResponse>> completableFuture =
subject.execute(requestInfo, executor, null);
completableFuture.join();
fail("Expected exception not thrown.");
} catch (CompletionException cex) {
assertThat(cex.getCause()).isInstanceOf(ApiException.class);
}
}
示例4: writeResponseHead
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Override
public void writeResponseHead(Response restletResponse) throws IOException {
setNettyResponse(new DefaultHttpResponse(HTTP_1_1, new HttpResponseStatus(getStatusCode(), getReasonPhrase())));
HttpHeaders headers = getNettyResponse().headers();
// this.response.clear();
for (Header header : getResponseHeaders()) {
headers.add(header.getName(), header.getValue());
}
// Decide whether to close the connection or not.
if (isKeepAlive()) {
headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
getNettyChannel().write(getNettyResponse());
} else {
getNettyChannel().writeAndFlush(getNettyResponse()).addListener(ChannelFutureListener.CLOSE);
}
}
示例5: getHeaderMapDelegatesToRequestInfoAndCachesResult
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Test
public void getHeaderMapDelegatesToRequestInfoAndCachesResult() {
Map<String, List<String>> expectedHeaderMap = new TreeMap<>(MapBuilder.<String, List<String>>builder()
.put("header1", Arrays.asList("h1val1"))
.put("header2", Arrays.asList("h2val1", "h2val2"))
.build());
HttpHeaders nettyHeaders = new DefaultHttpHeaders();
for (Map.Entry<String, List<String>> headerEntry : expectedHeaderMap.entrySet()) {
nettyHeaders.add(headerEntry.getKey(), headerEntry.getValue());
}
setFieldOnRequestInfo("headers", nettyHeaders);
Map<String, List<String>> actualHeaderMap = adapter.getHeadersMap();
assertThat(actualHeaderMap, is(expectedHeaderMap));
assertThat(adapter.getHeadersMap(), sameInstance(actualHeaderMap));
}
示例6: addHeader
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* Adding header to Http Carbon message
*
* @param httpCarbonMessage
*/
public static void addHeader(HTTPCarbonMessage httpCarbonMessage) {
String headerName = "";
String headerValue = "";
HttpHeaders httpHeaders = httpCarbonMessage.getHeaders();
httpHeaders.add(headerName, headerValue);
if (log.isDebugEnabled()) {
log.debug("Add " + headerName + " to header with value: " + headerValue);
}
}
示例7: setup
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Before
public void setup() throws Exception {
s = new Server(conf);
s.run();
Connector con = mac.getConnector("root", "secret");
con.securityOperations().changeUserAuthorizations("root", new Authorizations("A", "B", "C", "D", "E", "F"));
this.sessionId = UUID.randomUUID().toString();
AuthCache.getCache().put(sessionId, token);
group = new NioEventLoopGroup();
SslContext ssl = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
String cookieVal = ClientCookieEncoder.STRICT.encode(Constants.COOKIE_NAME, sessionId);
HttpHeaders headers = new DefaultHttpHeaders();
headers.add(Names.COOKIE, cookieVal);
WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(LOCATION,
WebSocketVersion.V13, (String) null, false, headers);
handler = new ClientHandler(handshaker);
Bootstrap boot = new Bootstrap();
boot.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("ssl", ssl.newHandler(ch.alloc(), "127.0.0.1", WS_PORT));
ch.pipeline().addLast(new HttpClientCodec());
ch.pipeline().addLast(new HttpObjectAggregator(8192));
ch.pipeline().addLast(handler);
}
});
ch = boot.connect("127.0.0.1", WS_PORT).sync().channel();
// Wait until handshake is complete
while (!handshaker.isHandshakeComplete()) {
sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
LOG.debug("Waiting for Handshake to complete");
}
}
示例8: test_validateSecureRequestForEndpoint_throws_error_when_vault_server_exception_caught
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Test(expected = ApiException.class)
public void test_validateSecureRequestForEndpoint_throws_error_when_vault_server_exception_caught() {
final RequestInfo<?> requestInfo = mock(RequestInfo.class);
final HttpHeaders httpHeaders = new DefaultHttpHeaders();
httpHeaders.add(CmsRequestSecurityValidator.HEADER_X_VAULT_TOKEN, vaultToken);
when(requestInfo.getHeaders()).thenReturn(httpHeaders);
when(vaultAdminClient.lookupToken(vaultToken)).thenThrow(new VaultServerException(1, Lists.newArrayList()));
subject.validateSecureRequestForEndpoint(requestInfo, securedEndpoint);
}
示例9: test_validateSecureRequestForEndpoint_throws_error_when_vault_client_exception_caught
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Test(expected = ApiException.class)
public void test_validateSecureRequestForEndpoint_throws_error_when_vault_client_exception_caught() {
final RequestInfo<?> requestInfo = mock(RequestInfo.class);
final HttpHeaders httpHeaders = new DefaultHttpHeaders();
httpHeaders.add(CmsRequestSecurityValidator.HEADER_X_VAULT_TOKEN, vaultToken);
when(requestInfo.getHeaders()).thenReturn(httpHeaders);
when(vaultAdminClient.lookupToken(vaultToken)).thenThrow(new VaultClientException("Failure"));
subject.validateSecureRequestForEndpoint(requestInfo, securedEndpoint);
}
示例10: test_getClientVersion
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Test
public void test_getClientVersion() {
String fakeVersion = "fake/1.2.3";
HttpHeaders headers = new DefaultHttpHeaders();
headers.add(CerberusHttpHeaders.HEADER_X_CERBERUS_CLIENT, fakeVersion);
RequestInfo request = mock(RequestInfo.class);
when(request.getHeaders()).thenReturn(headers);
Assert.assertEquals(fakeVersion, CerberusHttpHeaders.getClientVersion(request));
}
示例11: test_getXForwardedClientIp_with_three
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Test
public void test_getXForwardedClientIp_with_three() {
String value = "ip1, ip2, ip3";
HttpHeaders headers = new DefaultHttpHeaders();
headers.add(CerberusHttpHeaders.HEADER_X_FORWARDED_FOR, value);
RequestInfo request = mock(RequestInfo.class);
when(request.getHeaders()).thenReturn(headers);
Assert.assertEquals("ip1", CerberusHttpHeaders.getXForwardedClientIp(request));
}
示例12: test_getXForwardedClientIp_with_one
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Test
public void test_getXForwardedClientIp_with_one() {
String value = "ip1";
HttpHeaders headers = new DefaultHttpHeaders();
headers.add(CerberusHttpHeaders.HEADER_X_FORWARDED_FOR, value);
RequestInfo request = mock(RequestInfo.class);
when(request.getHeaders()).thenReturn(headers);
Assert.assertEquals("ip1", CerberusHttpHeaders.getXForwardedClientIp(request));
}
示例13: execute_returns_vault_auth_response
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Test
public void execute_returns_vault_auth_response() {
final AuthResponse authResponse = new AuthResponse();
final RequestInfo<Void> requestInfo = mock(RequestInfo.class);
final HttpHeaders httpHeaders = new DefaultHttpHeaders();
httpHeaders.add(HttpHeaders.Names.AUTHORIZATION, validAuthorizationHeader);
when(requestInfo.getHeaders()).thenReturn(httpHeaders);
when(authenticationService.authenticate(any(UserCredentials.class))).thenReturn(authResponse);
final CompletableFuture<ResponseInfo<AuthResponse>> completableFuture =
subject.execute(requestInfo, executor, null);
final ResponseInfo<AuthResponse> responseInfo = completableFuture.join();
assertThat(responseInfo.getContentForFullResponse()).isEqualTo(authResponse);
}
示例14: testOnHeadersReceivedContentLengthMinus
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Test
public void testOnHeadersReceivedContentLengthMinus() throws Exception {
ResumableAsyncHandler handler = new ResumableAsyncHandler();
HttpHeaders responseHeaders = new DefaultHttpHeaders();
responseHeaders.add(HttpHeaders.Names.CONTENT_LENGTH, -1);
HttpResponseHeaders headers = new HttpResponseHeaders(responseHeaders);
State status = handler.onHeadersReceived(headers);
assertEquals(status, AsyncHandler.State.ABORT, "State should be ABORT for content length -1");
}
示例15: copyHeaders
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
static void copyHeaders(HttpServletRequest request, HttpHeaders to) {
Enumeration<String> headers = request.getHeaderNames();
while(headers.hasMoreElements()) {
String header = headers.nextElement();
Iterable<String> iter = () -> Iterators.forEnumeration(request.getHeaders(header));
to.add(header, iter);
}
}