-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Escape html special characters in default error response generator.
- Loading branch information
1 parent
608ec08
commit 310a046
Showing
2 changed files
with
108 additions
and
1 deletion.
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
70 changes: 70 additions & 0 deletions
70
.../test/java/io/reactivex/netty/protocol/http/server/DefaultErrorResponseGeneratorTest.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,70 @@ | ||
package io.reactivex.netty.protocol.http.server; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.handler.codec.http.HttpResponseStatus; | ||
import io.reactivex.netty.RxNetty; | ||
import io.reactivex.netty.protocol.http.client.HttpClientRequest; | ||
import io.reactivex.netty.protocol.http.client.HttpClientResponse; | ||
import io.reactivex.netty.server.RxServer; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.matchers.JUnitMatchers; | ||
import rx.Observable; | ||
import rx.functions.Func1; | ||
import rx.functions.Func2; | ||
|
||
import java.nio.charset.Charset; | ||
|
||
public class DefaultErrorResponseGeneratorTest { | ||
private RxServer<HttpServerRequest<ByteBuf>, HttpServerResponse<ByteBuf>> server; | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
if (null != server) { | ||
server.shutdown(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testErrorGenerator() throws Exception { | ||
server = RxNetty.createHttpServer(0, new RequestHandler<ByteBuf, ByteBuf>() { | ||
@Override | ||
public Observable<Void> handle( | ||
HttpServerRequest<ByteBuf> request, | ||
HttpServerResponse<ByteBuf> response) { | ||
return Observable | ||
.error(new IllegalStateException( | ||
"I always throw an error<>'&\"{}.")); | ||
} | ||
}).start(); | ||
|
||
int port = server.getServerPort(); | ||
|
||
HttpClientRequest<ByteBuf> request = | ||
HttpClientRequest.createGet("/"); | ||
|
||
String html = | ||
RxNetty.createHttpClient("localhost", port).submit(request) | ||
.flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<String>>() { | ||
@Override | ||
public Observable<String> call(HttpClientResponse<ByteBuf> response) { | ||
return response.getContent().map(new Func1<ByteBuf, String>() { | ||
@Override | ||
public String call(ByteBuf byteBuf) { | ||
return byteBuf.toString(Charset.forName("ASCII")); | ||
} | ||
}).reduce(new Func2<String, String, String>() { | ||
@Override | ||
public String call(String s, String s2) { | ||
return s + s2; | ||
} | ||
}); | ||
} | ||
}).toBlocking().last(); | ||
|
||
Assert.assertThat("Unexpected response status", | ||
html, | ||
JUnitMatchers.containsString("I always throw an error<>'&"{}.\n")); | ||
} | ||
} |