|
| 1 | +package org.jooby.internal; |
| 2 | + |
| 3 | +import static org.easymock.EasyMock.expect; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.fail; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.charset.Charset; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.util.Locale; |
| 11 | +import java.util.Optional; |
| 12 | + |
| 13 | +import org.jooby.Err; |
| 14 | +import org.jooby.Route; |
| 15 | +import org.jooby.spi.NativeRequest; |
| 16 | +import org.jooby.test.MockUnit; |
| 17 | +import org.jooby.test.MockUnit.Block; |
| 18 | +import org.junit.Test; |
| 19 | + |
| 20 | +import com.google.common.collect.ImmutableList; |
| 21 | +import com.google.common.collect.ImmutableMap; |
| 22 | +import com.google.inject.Injector; |
| 23 | + |
| 24 | +public class RequestImplTest { |
| 25 | + |
| 26 | + private Block accept = unit -> { |
| 27 | + NativeRequest req = unit.get(NativeRequest.class); |
| 28 | + expect(req.header("Accept")).andReturn(Optional.of("*/*")); |
| 29 | + }; |
| 30 | + |
| 31 | + private Block contentType = unit -> { |
| 32 | + NativeRequest req = unit.get(NativeRequest.class); |
| 33 | + expect(req.header("Content-Type")).andReturn(Optional.empty()); |
| 34 | + }; |
| 35 | + |
| 36 | + private Block acceptLan = unit -> { |
| 37 | + NativeRequest req = unit.get(NativeRequest.class); |
| 38 | + expect(req.header("Accept-Language")).andReturn(Optional.empty()); |
| 39 | + }; |
| 40 | + |
| 41 | + private Block locale = unit -> { |
| 42 | + Injector injector = unit.get(Injector.class); |
| 43 | + expect(injector.getInstance(Locale.class)).andReturn(Locale.getDefault()); |
| 44 | + }; |
| 45 | + |
| 46 | + private Block charset = unit -> { |
| 47 | + Injector injector = unit.get(Injector.class); |
| 48 | + expect(injector.getInstance(Charset.class)).andReturn(StandardCharsets.UTF_8); |
| 49 | + }; |
| 50 | + |
| 51 | + @Test |
| 52 | + public void defaults() throws Exception { |
| 53 | + new MockUnit(Injector.class, NativeRequest.class, Route.class) |
| 54 | + .expect(accept) |
| 55 | + .expect(locale) |
| 56 | + .expect(acceptLan) |
| 57 | + .expect(contentType) |
| 58 | + .expect(charset) |
| 59 | + .run(unit -> { |
| 60 | + new RequestImpl(unit.get(Injector.class), unit.get(NativeRequest.class), "/", 8080, |
| 61 | + unit.get(Route.class), ImmutableMap.of(), ImmutableMap.of()); |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void matches() throws Exception { |
| 67 | + new MockUnit(Injector.class, NativeRequest.class, Route.class) |
| 68 | + .expect(accept) |
| 69 | + .expect(locale) |
| 70 | + .expect(acceptLan) |
| 71 | + .expect(contentType) |
| 72 | + .expect(charset) |
| 73 | + .expect(unit -> { |
| 74 | + Route route = unit.get(Route.class); |
| 75 | + expect(route.path()).andReturn("/path/x"); |
| 76 | + }) |
| 77 | + |
| 78 | + .run(unit -> { |
| 79 | + RequestImpl req = new RequestImpl(unit.get(Injector.class), unit.get(NativeRequest.class), |
| 80 | + "/", 8080, |
| 81 | + unit.get(Route.class), ImmutableMap.of(), ImmutableMap.of()); |
| 82 | + assertEquals(true, req.matches("/path/**")); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void lang() throws Exception { |
| 88 | + new MockUnit(Injector.class, NativeRequest.class, Route.class) |
| 89 | + .expect(accept) |
| 90 | + .expect(locale) |
| 91 | + .expect(unit -> { |
| 92 | + NativeRequest req = unit.get(NativeRequest.class); |
| 93 | + expect(req.header("Accept-Language")).andReturn(Optional.of("en")); |
| 94 | + }) |
| 95 | + .expect(contentType) |
| 96 | + .expect(charset) |
| 97 | + .run(unit -> { |
| 98 | + RequestImpl req = new RequestImpl(unit.get(Injector.class), unit.get(NativeRequest.class), |
| 99 | + "/", 8080, |
| 100 | + unit.get(Route.class), ImmutableMap.of(), ImmutableMap.of()); |
| 101 | + assertEquals(Locale.ENGLISH, req.locale()); |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void files() throws Exception { |
| 107 | + IOException cause = new IOException("intentional err"); |
| 108 | + new MockUnit(Injector.class, NativeRequest.class, Route.class) |
| 109 | + .expect(accept) |
| 110 | + .expect(locale) |
| 111 | + .expect(acceptLan) |
| 112 | + .expect(contentType) |
| 113 | + .expect(charset) |
| 114 | + .expect(unit -> { |
| 115 | + NativeRequest req = unit.get(NativeRequest.class); |
| 116 | + expect(req.files("f")).andThrow(cause); |
| 117 | + }) |
| 118 | + .run(unit -> { |
| 119 | + try { |
| 120 | + new RequestImpl(unit.get(Injector.class), unit.get(NativeRequest.class), "/", 8080, |
| 121 | + unit.get(Route.class), ImmutableMap.of(), ImmutableMap.of()).param("f"); |
| 122 | + fail("expecting error"); |
| 123 | + } catch (Err ex) { |
| 124 | + assertEquals(400, ex.statusCode()); |
| 125 | + assertEquals(cause, ex.getCause()); |
| 126 | + } |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void paramNames() throws Exception { |
| 132 | + IOException cause = new IOException("intentional err"); |
| 133 | + new MockUnit(Injector.class, NativeRequest.class, Route.class) |
| 134 | + .expect(accept) |
| 135 | + .expect(locale) |
| 136 | + .expect(acceptLan) |
| 137 | + .expect(contentType) |
| 138 | + .expect(charset) |
| 139 | + .expect(unit -> { |
| 140 | + Route route = unit.get(Route.class); |
| 141 | + expect(route.vars()).andReturn(ImmutableMap.of()); |
| 142 | + |
| 143 | + NativeRequest req = unit.get(NativeRequest.class); |
| 144 | + expect(req.paramNames()).andThrow(cause); |
| 145 | + }) |
| 146 | + .run(unit -> { |
| 147 | + try { |
| 148 | + new RequestImpl(unit.get(Injector.class), unit.get(NativeRequest.class), "/", 8080, |
| 149 | + unit.get(Route.class), ImmutableMap.of(), ImmutableMap.of()).params(); |
| 150 | + fail("expecting error"); |
| 151 | + } catch (Err ex) { |
| 152 | + assertEquals(400, ex.statusCode()); |
| 153 | + assertEquals(cause, ex.getCause()); |
| 154 | + } |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void params() throws Exception { |
| 160 | + IOException cause = new IOException("intentional err"); |
| 161 | + new MockUnit(Injector.class, NativeRequest.class, Route.class) |
| 162 | + .expect(accept) |
| 163 | + .expect(locale) |
| 164 | + .expect(acceptLan) |
| 165 | + .expect(contentType) |
| 166 | + .expect(charset) |
| 167 | + .expect(unit -> { |
| 168 | + Route route = unit.get(Route.class); |
| 169 | + expect(route.vars()).andReturn(ImmutableMap.of()); |
| 170 | + |
| 171 | + NativeRequest req = unit.get(NativeRequest.class); |
| 172 | + expect(req.files("p")).andReturn(ImmutableList.of()); |
| 173 | + expect(req.params("p")).andThrow(cause); |
| 174 | + }) |
| 175 | + .run(unit -> { |
| 176 | + try { |
| 177 | + new RequestImpl(unit.get(Injector.class), unit.get(NativeRequest.class), "/", 8080, |
| 178 | + unit.get(Route.class), ImmutableMap.of(), ImmutableMap.of()).param("p"); |
| 179 | + fail("expecting error"); |
| 180 | + } catch (Err ex) { |
| 181 | + assertEquals(400, ex.statusCode()); |
| 182 | + assertEquals(cause, ex.getCause()); |
| 183 | + } |
| 184 | + }); |
| 185 | + } |
| 186 | + |
| 187 | +} |
0 commit comments