forked from ReactiveX/RxNetty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HttpServer now handles HTTP 1.0 requests as per the spec.
- Loading branch information
Nitesh Kant
committed
May 23, 2014
1 parent
f19b37b
commit be72f7f
Showing
4 changed files
with
111 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
rx-netty/src/test/java/io/reactivex/netty/protocol/http/server/Http10Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright 2014 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.reactivex.netty.protocol.http.server; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.handler.codec.http.HttpMethod; | ||
import io.netty.handler.codec.http.HttpVersion; | ||
import io.netty.handler.logging.LogLevel; | ||
import io.reactivex.netty.RxNetty; | ||
import io.reactivex.netty.protocol.http.client.HttpClientRequest; | ||
import io.reactivex.netty.protocol.http.client.HttpClientResponse; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import rx.Observable; | ||
import rx.functions.Func1; | ||
|
||
import java.nio.charset.Charset; | ||
|
||
/** | ||
* @author Nitesh Kant | ||
*/ | ||
public class Http10Test { | ||
|
||
public static final String WELCOME_SERVER_MSG = "Welcome!"; | ||
private HttpServer<ByteBuf, ByteBuf> mockServer; | ||
private int mockServerPort; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
mockServer = RxNetty.createHttpServer(0, new RequestHandler<ByteBuf, ByteBuf>() { | ||
@Override | ||
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) { | ||
return response.writeStringAndFlush(WELCOME_SERVER_MSG); | ||
} | ||
}); | ||
mockServer.start(); | ||
mockServerPort = mockServer.getServerPort(); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
if (null != mockServer) { | ||
mockServer.shutdown(); | ||
mockServer.waitTillShutdown(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testHttp1_0Request() throws Exception { | ||
HttpClientRequest<ByteBuf> request = HttpClientRequest.create(HttpVersion.HTTP_1_0, HttpMethod.GET, "/"); | ||
HttpClientResponse<ByteBuf> response = RxNetty.<ByteBuf, ByteBuf>newHttpClientBuilder("localhost", mockServerPort) | ||
.enableWireLogging(LogLevel.ERROR).build() | ||
.submit(request).toBlockingObservable().last(); | ||
HttpVersion httpVersion = response.getHttpVersion(); | ||
Assert.assertEquals("Unexpected HTTP version.", HttpVersion.HTTP_1_1, httpVersion); | ||
Assert.assertFalse("Unexpected Connection header.", response.getHeaders().isKeepAlive()); | ||
Assert.assertFalse("Unexpected Transfer encoding.", response.getHeaders().isTransferEncodingChunked()); | ||
} | ||
|
||
@Test | ||
public void testHttp1_0RequestWithContent() throws Exception { | ||
HttpClientRequest<ByteBuf> request = HttpClientRequest.create(HttpVersion.HTTP_1_0, HttpMethod.GET, "/"); | ||
final ByteBuf response = RxNetty.<ByteBuf, ByteBuf>newHttpClientBuilder("localhost", mockServerPort) | ||
.enableWireLogging(LogLevel.ERROR).build() | ||
.submit(request) | ||
.flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<ByteBuf>>() { | ||
@Override | ||
public Observable<ByteBuf> call(HttpClientResponse<ByteBuf> response) { | ||
return response.getContent(); | ||
} | ||
}).toBlockingObservable().last(); | ||
Assert.assertEquals("Unexpected Content.", WELCOME_SERVER_MSG, response.toString(Charset.defaultCharset())); | ||
} | ||
} |