Search in sources :

Example 1 with HttpRequest

use of com.blade.mvc.http.HttpRequest in project blade by biezhi.

the class HttpRequestTest method testUri.

@Test
public void testUri() throws Exception {
    Request mockRequest = mockHttpRequest("GET");
    when(mockRequest.url()).thenReturn("/a");
    Request request = new HttpRequest(mockRequest);
    assertEquals("/a", request.uri());
    when(mockRequest.url()).thenReturn("/a/b?username=jack");
    request = new HttpRequest(mockRequest);
    assertEquals("/a/b", request.uri());
}
Also used : HttpRequest(com.blade.mvc.http.HttpRequest) HttpRequest(com.blade.mvc.http.HttpRequest) Request(com.blade.mvc.http.Request) Test(org.junit.Test)

Example 2 with HttpRequest

use of com.blade.mvc.http.HttpRequest in project blade by biezhi.

the class HttpRequestTest method testQueryParam.

@Test
public void testQueryParam() {
    Request mockRequest = mockHttpRequest("GET");
    Map<String, List<String>> parameters = new HashMap<>();
    parameters.put("name", Collections.singletonList("jack"));
    parameters.put("price", Collections.singletonList("22.1"));
    parameters.put("age", Collections.singletonList("25"));
    parameters.put("id", Collections.singletonList("220291"));
    when(mockRequest.parameters()).thenReturn(parameters);
    Request request = new HttpRequest(mockRequest);
    assertEquals("jack", request.query("name").get());
    assertEquals(Double.valueOf(22.1), request.queryDouble("price").get());
    assertEquals(Long.valueOf(220291), request.queryLong("id").get());
    assertEquals(Integer.valueOf(25), request.queryInt("age").get());
}
Also used : HttpRequest(com.blade.mvc.http.HttpRequest) HashMap(java.util.HashMap) CaseInsensitiveHashMap(com.blade.kit.CaseInsensitiveHashMap) HttpRequest(com.blade.mvc.http.HttpRequest) Request(com.blade.mvc.http.Request) List(java.util.List) Test(org.junit.Test)

Example 3 with HttpRequest

use of com.blade.mvc.http.HttpRequest in project blade by biezhi.

the class HttpRequestTest method testPathParam.

@Test
public void testPathParam() {
    Request mockRequest = mockHttpRequest("GET");
    Map<String, String> pathParams = new HashMap<>();
    pathParams.put("id", "6");
    pathParams.put("age", "24");
    pathParams.put("name", "jack");
    when(mockRequest.pathParams()).thenReturn(pathParams);
    Request request = new HttpRequest(mockRequest);
    assertEquals(Long.valueOf(6), request.pathLong("id"));
    assertEquals(Integer.valueOf(24), request.pathInt("age"));
    assertEquals("jack", request.pathString("name"));
}
Also used : HttpRequest(com.blade.mvc.http.HttpRequest) HashMap(java.util.HashMap) CaseInsensitiveHashMap(com.blade.kit.CaseInsensitiveHashMap) HttpRequest(com.blade.mvc.http.HttpRequest) Request(com.blade.mvc.http.Request) Test(org.junit.Test)

Example 4 with HttpRequest

use of com.blade.mvc.http.HttpRequest in project blade by biezhi.

the class HttpRequestTest method testIsIE.

@Test
public void testIsIE() {
    Request mockRequest = mockHttpRequest("GET");
    Map<String, String> headers = Collections.singletonMap("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
    when(mockRequest.headers()).thenReturn(headers);
    Request request = new HttpRequest(mockRequest);
    assertEquals(Boolean.TRUE, request.isIE());
    when(mockRequest.headers()).thenReturn(Collections.EMPTY_MAP);
    request = new HttpRequest(mockRequest);
    assertEquals(Boolean.FALSE, request.isIE());
}
Also used : HttpRequest(com.blade.mvc.http.HttpRequest) HttpRequest(com.blade.mvc.http.HttpRequest) Request(com.blade.mvc.http.Request) Test(org.junit.Test)

Example 5 with HttpRequest

use of com.blade.mvc.http.HttpRequest in project blade by biezhi.

the class HttpRequestTest method testHost.

@Test
public void testHost() {
    HttpRequest request = mockHttpRequest("GET");
    when(request.host()).thenReturn("127.0.0.1");
    Assert.assertEquals(request.host(), "127.0.0.1");
    when(request.host()).thenReturn("localhost");
    Assert.assertEquals(request.host(), "localhost");
}
Also used : HttpRequest(com.blade.mvc.http.HttpRequest) Test(org.junit.Test)

Aggregations

HttpRequest (com.blade.mvc.http.HttpRequest)15 Test (org.junit.Test)14 Request (com.blade.mvc.http.Request)12 HashMap (java.util.HashMap)7 CaseInsensitiveHashMap (com.blade.kit.CaseInsensitiveHashMap)6 RouteContext (com.blade.mvc.RouteContext)2 WebContext (com.blade.mvc.WebContext)2 Response (com.blade.mvc.http.Response)2 AuthOption (com.blade.security.web.auth.AuthOption)2 BasicAuthMiddleware (com.blade.security.web.auth.BasicAuthMiddleware)2 Cookie (com.blade.mvc.http.Cookie)1 FileItem (com.blade.mvc.multipart.FileItem)1 io.netty.handler.codec.http (io.netty.handler.codec.http)1 List (java.util.List)1