Search in sources :

Example 11 with HttpRequest

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

the class HttpRequestTest method testUserAgent.

@Test
public void testUserAgent() {
    Map<String, String> headers = Collections.singletonMap("User-Agent", firefoxUA);
    Request mockRequest = mockHttpRequest("GET");
    when(mockRequest.headers()).thenReturn(headers);
    Request request = new HttpRequest(mockRequest);
    assertEquals(firefoxUA, request.userAgent());
}
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 12 with HttpRequest

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

the class HttpRequestTest method testIsAjax.

@Test
public void testIsAjax() {
    Request mockRequest = mockHttpRequest("GET");
    Map<String, String> headers = Collections.singletonMap("x-requested-with", "XMLHttpRequest");
    when(mockRequest.headers()).thenReturn(headers);
    Request request = new HttpRequest(mockRequest);
    assertEquals(Boolean.TRUE, request.isAjax());
    when(mockRequest.headers()).thenReturn(Collections.EMPTY_MAP);
    request = new HttpRequest(mockRequest);
    assertEquals(Boolean.FALSE, request.isAjax());
}
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 13 with HttpRequest

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

the class HttpRequestTest method testHeaders.

@Test
public void testHeaders() {
    Request mockRequest = mockHttpRequest("GET");
    Map<String, String> headers = new CaseInsensitiveHashMap<>();
    headers.put("h1", "a1");
    headers.put("H2", "a2");
    when(mockRequest.headers()).thenReturn(headers);
    Request request = new HttpRequest(mockRequest);
    assertEquals("a1", request.header("h1"));
    assertEquals("a1", request.header("H1"));
    assertEquals("a2", request.header("h2"));
    assertEquals("a2", request.header("H2"));
    request.headers().forEach((key, val) -> System.out.println(key + "\t=\t" + val));
}
Also used : HttpRequest(com.blade.mvc.http.HttpRequest) HttpRequest(com.blade.mvc.http.HttpRequest) Request(com.blade.mvc.http.Request) CaseInsensitiveHashMap(com.blade.kit.CaseInsensitiveHashMap) Test(org.junit.Test)

Example 14 with HttpRequest

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

the class HttpRequestTest method testQueryString.

@Test
public void testQueryString() {
    Request mockRequest = mockHttpRequest("GET");
    when(mockRequest.url()).thenReturn("/hello?name=q1");
    Request request = new HttpRequest(mockRequest);
    assertEquals("name=q1", request.queryString());
}
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 15 with HttpRequest

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

the class HttpRequestTest method testCookie.

@Test
public void testCookie() {
    Map<String, Cookie> cookieMap = new HashMap<>();
    Cookie c1 = new Cookie();
    c1.name("c1");
    c1.value("hello1");
    cookieMap.put("c1", c1);
    Cookie c2 = new Cookie();
    c2.name("c1");
    c2.value("hello1");
    c2.httpOnly(true);
    cookieMap.put("c2", c2);
    Cookie c3 = new Cookie();
    c3.name("c3");
    c3.value("hello3");
    c3.secure(false);
    cookieMap.put("c3", c3);
    Cookie c4 = new Cookie();
    c4.name("c4");
    c4.value("hello4");
    c4.domain("www.github.com");
    c4.path("/github");
    cookieMap.put("c4", c4);
    HttpRequest request = mockHttpRequest("GET");
    when(request.cookies()).thenReturn(cookieMap);
    when(request.cookie("c1")).thenReturn(cookieMap.get("c1").value());
    when(request.cookieRaw("c2")).thenReturn(cookieMap.get("c2"));
    when(request.cookieRaw("c3")).thenReturn(cookieMap.get("c3"));
    when(request.cookieRaw("c4")).thenReturn(cookieMap.get("c4"));
    Assert.assertEquals(request.cookies(), cookieMap);
    Assert.assertEquals(request.cookies().size(), cookieMap.size());
    Assert.assertEquals(request.cookie("c1"), "hello1");
    Assert.assertTrue(request.cookieRaw("c2").httpOnly());
    Assert.assertFalse(request.cookieRaw("c3").secure());
    Assert.assertEquals(request.cookieRaw("c3").path(), "/");
    Assert.assertEquals(request.cookieRaw("c4").domain(), "www.github.com");
    Assert.assertEquals(request.cookieRaw("c4").path(), "/github");
}
Also used : Cookie(com.blade.mvc.http.Cookie) HttpRequest(com.blade.mvc.http.HttpRequest) HashMap(java.util.HashMap) CaseInsensitiveHashMap(com.blade.kit.CaseInsensitiveHashMap) 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