|
| 1 | +package org.jooby.internal.handlers; |
| 2 | + |
| 3 | +import static org.easymock.EasyMock.expect; |
| 4 | + |
| 5 | +import java.util.Optional; |
| 6 | +import java.util.Set; |
| 7 | + |
| 8 | +import org.jooby.MediaType; |
| 9 | +import org.jooby.Request; |
| 10 | +import org.jooby.Response; |
| 11 | +import org.jooby.Route; |
| 12 | +import org.jooby.Route.Chain; |
| 13 | +import org.jooby.Route.Definition; |
| 14 | +import org.jooby.internal.RouteImpl; |
| 15 | +import org.jooby.test.MockUnit; |
| 16 | +import org.jooby.test.MockUnit.Block; |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +import com.google.common.collect.Sets; |
| 20 | + |
| 21 | +public class HeadHandlerTest { |
| 22 | + |
| 23 | + private Block path = unit -> { |
| 24 | + Request req = unit.get(Request.class); |
| 25 | + expect(req.path()).andReturn("/"); |
| 26 | + }; |
| 27 | + |
| 28 | + private Block len = unit -> { |
| 29 | + Response rsp = unit.get(Response.class); |
| 30 | + expect(rsp.length(0)).andReturn(rsp); |
| 31 | + }; |
| 32 | + |
| 33 | + private Block next = unit -> { |
| 34 | + Chain chain = unit.get(Route.Chain.class); |
| 35 | + chain.next(unit.get(Request.class), unit.get(Response.class)); |
| 36 | + }; |
| 37 | + |
| 38 | + @Test |
| 39 | + public void handle() throws Exception { |
| 40 | + new MockUnit(Request.class, Response.class, Route.Chain.class, Route.Definition.class) |
| 41 | + .expect(path) |
| 42 | + .expect(unit -> { |
| 43 | + Route.Definition routeDef = unit.get(Route.Definition.class); |
| 44 | + expect(routeDef.glob()).andReturn(false); |
| 45 | + |
| 46 | + RouteImpl route = unit.mock(RouteImpl.class); |
| 47 | + route.handle(unit.get(Request.class), unit.get(Response.class), |
| 48 | + unit.get(Route.Chain.class)); |
| 49 | + |
| 50 | + Optional<Route> ifRoute = Optional.of(route); |
| 51 | + |
| 52 | + expect(routeDef.matches(Route.GET, "/", MediaType.all, MediaType.ALL)).andReturn(ifRoute); |
| 53 | + }) |
| 54 | + .expect(len) |
| 55 | + .run(unit -> { |
| 56 | + Set<Definition> routes = Sets.newHashSet(unit.get(Route.Definition.class)); |
| 57 | + new HeadHandler(routes) |
| 58 | + .handle(unit.get(Request.class), unit.get(Response.class), |
| 59 | + unit.get(Route.Chain.class)); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void noRoute() throws Exception { |
| 65 | + new MockUnit(Request.class, Response.class, Route.Chain.class, Route.Definition.class) |
| 66 | + .expect(path) |
| 67 | + .expect(unit -> { |
| 68 | + Route.Definition routeDef = unit.get(Route.Definition.class); |
| 69 | + expect(routeDef.glob()).andReturn(false); |
| 70 | + |
| 71 | + Optional<Route> ifRoute = Optional.empty(); |
| 72 | + |
| 73 | + expect(routeDef.matches(Route.GET, "/", MediaType.all, MediaType.ALL)).andReturn(ifRoute); |
| 74 | + }) |
| 75 | + .expect(next) |
| 76 | + .run(unit -> { |
| 77 | + Set<Definition> routes = Sets.newHashSet(unit.get(Route.Definition.class)); |
| 78 | + new HeadHandler(routes) |
| 79 | + .handle(unit.get(Request.class), unit.get(Response.class), |
| 80 | + unit.get(Route.Chain.class)); |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void ignoreGlob() throws Exception { |
| 86 | + new MockUnit(Request.class, Response.class, Route.Chain.class, Route.Definition.class) |
| 87 | + .expect(path) |
| 88 | + .expect(unit -> { |
| 89 | + Route.Definition routeDef = unit.get(Route.Definition.class); |
| 90 | + expect(routeDef.glob()).andReturn(true); |
| 91 | + }) |
| 92 | + .expect(next) |
| 93 | + .run(unit -> { |
| 94 | + Set<Definition> routes = Sets.newHashSet(unit.get(Route.Definition.class)); |
| 95 | + new HeadHandler(routes) |
| 96 | + .handle(unit.get(Request.class), unit.get(Response.class), |
| 97 | + unit.get(Route.Chain.class)); |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void noroutes() throws Exception { |
| 103 | + new MockUnit(Request.class, Response.class, Route.Chain.class, Route.Definition.class) |
| 104 | + .expect(path) |
| 105 | + .expect(next) |
| 106 | + .run(unit -> { |
| 107 | + Set<Definition> routes = Sets.newHashSet(); |
| 108 | + new HeadHandler(routes) |
| 109 | + .handle(unit.get(Request.class), unit.get(Response.class), |
| 110 | + unit.get(Route.Chain.class)); |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments