Skip to content

Commit

Permalink
#66 got the basic unit level tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
robfletcher committed Aug 1, 2013
1 parent 95b1427 commit 857c8fb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -32,9 +38,6 @@ class BetamaxProxySpec extends Specification {
def handler = Mock(HttpHandler)
proxy << handler

and:
def context = Stub(ChannelHandlerContext)

when:
proxy.channelRead(context, request)

Expand All @@ -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'() {
Expand All @@ -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
Expand All @@ -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
}

}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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'() {
Expand Down

0 comments on commit 857c8fb

Please sign in to comment.