Package com.gargoylesoftware.htmlunit

Examples of com.gargoylesoftware.htmlunit.WebResponse


    private OAuthToken getAccessToken(String refreshToken) throws IOException {
        final String tokenUrl = String.format(ACCESS_TOKEN_URL, refreshToken,
            oAuthParams.getRedirectUri(), oAuthParams.getClientId(), oAuthParams.getClientSecret());
        final WebRequest webRequest = new WebRequest(new URL(tokenUrl), HttpMethod.POST);

        final WebResponse webResponse = webClient.loadWebResponse(webRequest);
        if (webResponse.getStatusCode() != HttpStatus.SC_OK) {
            throw new IOException(String.format("Error getting access token: [%s: %s]",
                webResponse.getStatusCode(), webResponse.getStatusMessage()));
        }
        final long currentTime = System.currentTimeMillis();
        final ObjectMapper mapper = new ObjectMapper();
        final Map map = mapper.readValue(webResponse.getContentAsStream(), Map.class);
        final String accessToken = map.get("access_token").toString();
        final Integer expiresIn = Integer.valueOf(map.get("expires_in").toString());
        return new OAuthToken(refreshToken, accessToken,
            currentTime + TimeUnit.MILLISECONDS.convert(expiresIn, TimeUnit.SECONDS));
    }
View Full Code Here


        WebRequest webRequest = new WebRequest(new URL(
            "http://localhost/rfRes/org.richfaces.resource.CacheableResourceImpl.jsf"));

        webRequest.getAdditionalHeaders().put(ECHO_HEADER, "ping?");

        WebResponse webResponse = webClient.loadWebResponse(webRequest);

        assertEquals(HttpServletResponse.SC_OK, webResponse.getStatusCode());
        assertEquals(ResourceUtils.formatHttpDate(lastModified.getTime()), webResponse.getResponseHeaderValue("Last-Modified"));
        assertEquals(ResourceUtils.formatHttpDate(currentTime.getTime()), webResponse.getResponseHeaderValue("Date"));
        assertEquals(ResourceUtils.formatHttpDate(expires.getTime()), webResponse.getResponseHeaderValue("Expires"));
        assertEquals("max-age=1209600", webResponse.getResponseHeaderValue("Cache-Control"));
        assertTrue(webResponse.getResponseHeaderValue("Content-Type").startsWith("text/plain"));
        assertEquals("W/\"" + "ping?".length() + "-" + lastModified.getTime() + "\"",
            webResponse.getResponseHeaderValue("ETag"));
        assertNull(webResponse.getResponseHeaderValue("Pragma"));
        assertEquals("ping?", webResponse.getContentAsString("US-ASCII"));
        webRequest.getAdditionalHeaders().put(ECHO_HEADER, "pong");

        WebResponse cachedWebResponse = webClient.loadWebResponse(webRequest);

        assertEquals(HttpServletResponse.SC_OK, cachedWebResponse.getStatusCode());
        assertEquals("ping?", cachedWebResponse.getContentAsString("US-ASCII"));
        webRequest.getAdditionalHeaders().put(IF_MODIFIED_SINCE, ResourceUtils.formatHttpDate(currentTime));

        WebResponse cachedWebResponse2 = webClient.loadWebResponse(webRequest);

        assertEquals(HttpServletResponse.SC_NOT_MODIFIED, cachedWebResponse2.getStatusCode());
    }
View Full Code Here

        WebRequest webRequest = new WebRequest(new URL(
            "http://localhost/rfRes/org.richfaces.resource.NonCacheableResourceImpl.jsf"));

        webRequest.getAdditionalHeaders().put(ECHO_HEADER, "ping?");

        WebResponse webResponse = webClient.loadWebResponse(webRequest);

        assertEquals(HttpServletResponse.SC_OK, webResponse.getStatusCode());
        assertEquals(ResourceUtils.formatHttpDate(lastModified.getTime()), webResponse.getResponseHeaderValue("Last-Modified"));
        assertEquals(ResourceUtils.formatHttpDate(currentTime.getTime()), webResponse.getResponseHeaderValue("Date"));
        assertEquals("0", webResponse.getResponseHeaderValue("Expires"));
        assertEquals("max-age=0, no-store, no-cache", webResponse.getResponseHeaderValue("Cache-Control"));
        assertTrue(webResponse.getResponseHeaderValue("Content-Type").startsWith("text/plain"));
        assertNull(webResponse.getResponseHeaderValue("ETag"));
        assertEquals("no-cache", webResponse.getResponseHeaderValue("Pragma"));
        assertEquals("ping?", webResponse.getContentAsString("US-ASCII"));
        webRequest.getAdditionalHeaders().put(ECHO_HEADER, "pong");

        WebResponse cachedWebResponse = webClient.loadWebResponse(webRequest);

        assertEquals(HttpServletResponse.SC_OK, cachedWebResponse.getStatusCode());
        assertEquals("pong", cachedWebResponse.getContentAsString("US-ASCII"));
        webRequest.getAdditionalHeaders().put(IF_MODIFIED_SINCE, ResourceUtils.formatHttpDate(currentTime));

        WebResponse cachedWebResponse2 = webClient.loadWebResponse(webRequest);

        assertEquals(HttpServletResponse.SC_OK, cachedWebResponse2.getStatusCode());
    }
View Full Code Here

        assertEquals(HttpServletResponse.SC_OK, cachedWebResponse2.getStatusCode());
    }

    public void testNonExistingResource() throws Exception {
        WebRequest emptyResourceNameSettings = new WebRequest(new URL("http://localhost/rfRes/x.jsf"));
        WebResponse emptyResourceNameResponse = webClient.loadWebResponse(emptyResourceNameSettings);

        assertEquals(HttpServletResponse.SC_NOT_FOUND, emptyResourceNameResponse.getStatusCode());
    }
View Full Code Here

    }

    public void testDefaultMojarraResource() throws Exception {
        WebRequest mojarraWebRequest = new WebRequest(new URL(
            "http://localhost/javax.faces.resource/defaultResourceHandlerResource.js.jsf"));
        WebResponse mojarraResourceNameResponse = webClient.loadWebResponse(mojarraWebRequest);

        assertEquals(HttpServletResponse.SC_OK, mojarraResourceNameResponse.getStatusCode());
    }
View Full Code Here

        assertNotNull(contextClassLoader
            .getResource("META-INF/org.richfaces.resource.MarkerFileResourceImpl.resource.properties"));

        WebRequest markerFileWebRequest = new WebRequest(new URL(
            "http://localhost/rfRes/org.richfaces.resource.MarkerFileResourceImpl.jsf"));
        WebResponse markerFileResponse = webClient.loadWebResponse(markerFileWebRequest);

        assertEquals(HttpServletResponse.SC_OK, markerFileResponse.getStatusCode());
        assertNotNull(Class.forName("org.richfaces.resource.NoMarkerFileResourceImpl", true, contextClassLoader));
        assertNull(contextClassLoader
            .getResource("META-INF/org.richfaces.resource.NoMarkerFileResourceImpl.resource.properties"));

        WebRequest noMarkerFileRequestSettings = new WebRequest(new URL(
            "http://localhost/rfRes/org.richfaces.resource.NoMarkerFileResourceImpl.jsf"));
        WebResponse noMarkerResponse = webClient.loadWebResponse(noMarkerFileRequestSettings);

        assertEquals(HttpServletResponse.SC_NOT_FOUND, noMarkerResponse.getStatusCode());
    }
View Full Code Here

            }
        }));
        ServiceTracker.setFactory(injector);

        WebRequest webRequest = new WebRequest(new URL("http://localhost/rfRes/StateHolderResource.jsf?db=1"));
        WebResponse resourceResponse = webClient.loadWebResponse(webRequest);

        assertEquals(HttpServletResponse.SC_OK, resourceResponse.getStatusCode());
        assertEquals("test text", resourceResponse.getContentAsString("US-ASCII"));

        EasyMock.verify(mockedCodec, resourceCodecData, mockCache);
    }
View Full Code Here

    }

    public void testVersionedResource() throws Exception {
        WebRequest webRequest = new WebRequest(new URL(
            "http://localhost/rfRes/org.richfaces.resource.VersionedResourceImpl.jsf"));
        WebResponse resourceResponse = webClient.loadWebResponse(webRequest);

        assertEquals(HttpServletResponse.SC_OK, resourceResponse.getStatusCode());
        webRequest = new WebRequest(new URL(
            "http://localhost/rfRes/org.richfaces.resource.VersionedResourceImpl.jsf?v=1_0_2"));
        resourceResponse = webClient.loadWebResponse(webRequest);
        assertEquals(HttpServletResponse.SC_OK, resourceResponse.getStatusCode());
        webRequest = new WebRequest(new URL(
            "http://localhost/rfRes/org.richfaces.resource.VersionedResourceImpl.jsf?v=1_0_3"));
        resourceResponse = webClient.loadWebResponse(webRequest);
        assertEquals(HttpServletResponse.SC_NOT_FOUND, resourceResponse.getStatusCode());
    }
View Full Code Here

        String resourceName = null;
        List<String> resources = populateResourcesToCheck();
        for (String resource : resources) {
            resourceName = baseResourseURL + resource + endResourceURL;
            WebRequest webRequest = new WebRequest(new URL(resourceName));
            WebResponse resourceResponse = webClient.loadWebResponse(webRequest);
            assertEquals(resource, HttpServletResponse.SC_OK, resourceResponse.getStatusCode());
            String expected = readFileAsString(getResourceExpectedOutputFileName(resource));

            assertEquals(resource, unifyWhitespace(expected), unifyWhitespace(resourceResponse.getContentAsString()));
        }
    }
View Full Code Here

        URL url = new URL(testUrl);
        HtmlPage page = (HtmlPage) client.getPage(url);
        // System.out.println(page.getTitleText());

        // Check response code
        WebResponse resp = page.getWebResponse();
        assertTrue(resp.getStatusCode() <= 200);
        List forms = page.getForms();
       

        HtmlPage resultPage = null;
        WebResponse resultResponse= null;
        boolean formfound = false;
        for (Iterator iter = forms.iterator(); formfound != true && iter.hasNext();) {
            HtmlForm form = (HtmlForm) iter.next();

            if ("loginAction".equals(form.getActionAttribute())) {
                formfound = true;
                HtmlInput login = form.getInputByName("login");
                HtmlInput password = form.getInputByName("password");
                login.setValueAttribute("test");
                password.setValueAttribute("password");

                resultPage = (HtmlPage) form.submit();
                // Check response from servlet
                resultResponse = resultPage.getWebResponse();
                assertTrue(resultResponse.getStatusCode() <= 200);

             
                String response = resultResponse.getContentAsString();
                assertTrue(-1 != response.indexOf("Account Information for"));
                assertTrue(-1 != response.indexOf("Test"));
                assertTrue(-1 != response.indexOf("134-42-6231"));
                assertTrue(-1 != response.indexOf("123.43"));
                assertTrue(-1 != response.indexOf("134-43-3941"));
                assertTrue(-1 != response.indexOf("23.12"));

                Pattern pat = Pattern.compile("LU.*7", Pattern.DOTALL);
                Matcher test = pat.matcher(response);
                assertTrue(test.find());
            }

        }
        forms = resultPage.getForms();
       
        formfound = false;
        for(Iterator iter = forms.iterator();  formfound != true && iter.hasNext();) {
            HtmlForm form = (HtmlForm) iter.next();

            if ("purchaseStock.jsp".equals(form.getActionAttribute())) {
                formfound = true;
                resultPage = (HtmlPage) form.submit();
            }

        }
        assertTrue(formfound);
        forms = resultPage.getForms();
        formfound = false;
        for (Iterator iter = forms.iterator(); formfound != true && iter.hasNext();) {
            HtmlForm form = (HtmlForm) iter.next();

            if ("FormServlet".equals(form.getActionAttribute())) {
                formfound = true;

                HtmlInput login = form.getInputByName("symbol");
                HtmlInput password = form.getInputByName("quantity");
                login.setValueAttribute("EDS");
                password.setValueAttribute("131");
               
                resultPage = (HtmlPage) form.submit();
                // Check response from servlet
                resultResponse = resultPage.getWebResponse();
                assertTrue(resultResponse.getStatusCode() <= 200);

            
                String response = resultResponse.getContentAsString();
                assertTrue(-1 != response.indexOf("Account Information for"));
                assertTrue(-1 != response.indexOf("Test"));
                assertTrue(-1 != response.indexOf("134-42-6231"));
                assertTrue(-1 != response.indexOf("123.43"));
                assertTrue(-1 != response.indexOf("134-43-3941"));
                assertTrue(-1 != response.indexOf("23.12"));

                Pattern pat = Pattern.compile("LU.*7", Pattern.DOTALL);
                Matcher test = pat.matcher(response);
                assertTrue(test.find());
                pat = Pattern.compile("EDS.*131", Pattern.DOTALL);
                test = pat.matcher(response);
                assertTrue(test.find());

            }

        }
        assertTrue(formfound);
        forms = resultPage.getForms();
        formfound = false;
        for (Iterator iter = forms.iterator(); formfound != true && iter.hasNext();) {
            HtmlForm form = (HtmlForm) iter.next();

            if ("stockSale.jsp".equals(form.getActionAttribute())) {
//                System.err.println(form.asText());
               
               
               
                Pattern pat = Pattern.compile("EDS.*131", Pattern.DOTALL);
               
                Matcher test = pat.matcher(form.asText());
                if(!test.find()) continue;

               
               
               
                formfound = true;
                resultPage = (HtmlPage) form.submit();
                // Check response from servlet
                resultResponse = resultPage.getWebResponse();
                assertTrue(resultResponse.getStatusCode() <= 200);


            }

        }
        forms = resultPage.getForms();
       
       
       
        formfound = false;
        for(Iterator iter = forms.iterator();  formfound != true && iter.hasNext();) {
            HtmlForm form = (HtmlForm) iter.next();

            if ("FormServlet".equals(form.getActionAttribute())) {
                formfound = true;

               
                HtmlInput password = form.getInputByName("quantity");
               
                password.setValueAttribute("131");

                resultPage = (HtmlPage) form.submit();
                // Check response from servlet
                resultResponse = resultPage.getWebResponse();
                assertTrue(resultResponse.getStatusCode() <= 200);

               
                String response = resultResponse.getContentAsString();
                assertTrue(-1 != response.indexOf("Account Information for"));
                assertTrue(-1 != response.indexOf("Test"));
                assertTrue(-1 != response.indexOf("134-42-6231"));
                assertTrue(-1 != response.indexOf("123.43"));
                assertTrue(-1 != response.indexOf("134-43-3941"));
View Full Code Here

TOP

Related Classes of com.gargoylesoftware.htmlunit.WebResponse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.