Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

#66 got the basic unit level tests working

  • Loading branch information...
commit 857c8fb8d9cbc951f75f6c7134afbb06c4bd07f0 1 parent 95b1427
Rob Fletcher authored
View
5 betamax-proxy/src/main/groovy/co/freeside/betamax/proxy/netty/BetamaxChannelHandler.groovy
@@ -19,7 +19,6 @@ package co.freeside.betamax.proxy.netty
import java.util.logging.Logger
import co.freeside.betamax.handler.*
import co.freeside.betamax.message.Response
-import co.freeside.betamax.message.servlet.NettyRequestAdapter
import io.netty.buffer.Unpooled
import io.netty.channel.*
import io.netty.handler.codec.http.*
@@ -79,13 +78,13 @@ public class BetamaxChannelHandler extends ChannelInboundHandlerAdapter {
def content = betamaxResponse.hasBody() ? Unpooled.copiedBuffer(betamaxResponse.bodyAsBinary.bytes) : Unpooled.EMPTY_BUFFER
FullHttpResponse response = betamaxResponse.hasBody() ? new DefaultFullHttpResponse(HTTP_1_1, status, content) : new DefaultFullHttpResponse(HTTP_1_1, status)
for (Map.Entry<String, String> header : betamaxResponse.headers) {
- response.headers().set(header.key, header.value.split(/,\s*/))
+ response.headers().set(header.key, header.value.split(/,\s*/).toList())
}
sendResponse(context, response)
}
private void sendError(ChannelHandlerContext context, HttpResponseStatus status, String message) {
- FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.copiedBuffer(message, CharsetUtil.UTF_8))
+ FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.copiedBuffer(message ?: "", CharsetUtil.UTF_8))
response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8")
sendResponse(context, response)
}
View
3  .../message/servlet/NettyRequestAdapter.java → ...amax/proxy/netty/NettyRequestAdapter.java
@@ -14,14 +14,13 @@
* limitations under the License.
*/
-package co.freeside.betamax.message.servlet;
+package co.freeside.betamax.proxy.netty;
import java.io.*;
import java.net.*;
import java.util.*;
import co.freeside.betamax.message.*;
import com.google.common.base.*;
-import com.google.common.collect.*;
import io.netty.handler.codec.http.*;
public class NettyRequestAdapter extends AbstractMessage implements Request {
View
48 ...tamax/proxy/netty/BetamaxProxySpec.groovy → ...xy/netty/BetamaxChannelHandlerSpec.groovy
@@ -2,23 +2,29 @@ package co.freeside.betamax.proxy.netty
import co.freeside.betamax.handler.*
import co.freeside.betamax.message.*
-import co.freeside.betamax.message.servlet.NettyRequestAdapter
import co.freeside.betamax.util.message.BasicResponse
import io.netty.buffer.Unpooled
-import io.netty.channel.ChannelHandlerContext
+import io.netty.channel.*
import io.netty.handler.codec.http.*
import spock.lang.Specification
import static io.netty.handler.codec.http.HttpHeaders.Names.ETAG
+import static io.netty.handler.codec.http.HttpMethod.GET
import static io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR
+import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1
import static io.netty.handler.codec.rtsp.RtspHeaders.Names.VIA
-class BetamaxProxySpec extends Specification {
+class BetamaxChannelHandlerSpec extends Specification {
BetamaxChannelHandler proxy = new BetamaxChannelHandler()
-
- HttpRequest request = [:] as HttpRequest
-
+ FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, "http://freeside.co/betamax/")
+ FullHttpResponse response = null
Response betamaxResponse
+ ChannelHandlerContext context = Stub(ChannelHandlerContext) {
+ writeAndFlush(_) >> {
+ response = it[0]
+ return new DefaultChannelPromise(context.channel())
+ }
+ }
void setup() {
betamaxResponse = new BasicResponse(200, 'OK')
@@ -32,9 +38,6 @@ class BetamaxProxySpec extends Specification {
def handler = Mock(HttpHandler)
proxy << handler
- and:
- def context = Stub(ChannelHandlerContext)
-
when:
proxy.channelRead(context, request)
@@ -52,19 +55,14 @@ class BetamaxProxySpec extends Specification {
handler.handle(_) >> betamaxResponse
proxy << handler
- and:
- def context = Mock(ChannelHandlerContext)
-
when:
proxy.channelRead(context, request)
then:
- 1 * context.writeAndFlush(_) >> { FullHttpResponse response ->
- response.status.code() == betamaxResponse.status
- response.headers().get(ETAG) == betamaxResponse.getHeader(ETAG)
- response.headers().getAll(VIA).containsAll(betamaxResponse.getHeader(VIA).split(/,\s*/))
- response.content() == Unpooled.copiedBuffer(betamaxResponse.bodyAsBinary.bytes)
- }
+ response.status.code() == betamaxResponse.status
+ response.headers().get(ETAG) == betamaxResponse.getHeader(ETAG)
+ response.headers().getAll(VIA).containsAll(betamaxResponse.getHeader(VIA).split(/,\s*/))
+ response.content() == Unpooled.copiedBuffer(betamaxResponse.bodyAsBinary.bytes)
}
void 'responds with the specified error status if the handler chain throws ProxyException'() {
@@ -73,16 +71,11 @@ class BetamaxProxySpec extends Specification {
handler.handle(_) >> { throw [getHttpStatus: {-> errorStatus}] as HandlerException }
proxy << handler
- and:
- def context = Mock(ChannelHandlerContext)
-
when:
proxy.channelRead(context, request)
then:
- 1 * context.writeAndFlush(_) >> { FullHttpResponse response ->
- response.status.code() == errorStatus
- }
+ response.status.code() == errorStatus
where:
errorStatus = 419
@@ -94,16 +87,11 @@ class BetamaxProxySpec extends Specification {
handler.handle(_) >> { throw new IllegalStateException() }
proxy << handler
- and:
- def context = Mock(ChannelHandlerContext)
-
when:
proxy.channelRead(context, request)
then:
- 1 * context.writeAndFlush(_) >> { FullHttpResponse response ->
- response.status == INTERNAL_SERVER_ERROR
- }
+ response.status == INTERNAL_SERVER_ERROR
}
}
View
5 ...ge/servlet/NettyRequestAdapterSpec.groovy → ...roxy/netty/NettyRequestAdapterSpec.groovy
@@ -1,8 +1,7 @@
-package co.freeside.betamax.message.servlet
+package co.freeside.betamax.proxy.netty
import io.netty.buffer.Unpooled
import io.netty.handler.codec.http.*
-import org.apache.commons.collections.iterators.*
import spock.lang.*
import static io.netty.handler.codec.http.HttpHeaders.Names.*
import static io.netty.handler.codec.http.HttpMethod.GET
@@ -11,8 +10,6 @@ import static io.netty.util.CharsetUtil.UTF_8
@Unroll
class NettyRequestAdapterSpec extends Specification {
- private static final IteratorEnumeration EMPTY_ENUMERATION = new IteratorEnumeration(EmptyIterator.INSTANCE)
-
FullHttpRequest nettyRequest = Mock(FullHttpRequest)
void 'request can read basic fields'() {

0 comments on commit 857c8fb

Please sign in to comment.
Something went wrong with that request. Please try again.