1424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 2424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// found in the LICENSE file. 4424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 5424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)package org.chromium.chrome.test.util; 6424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 7424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)import junit.framework.Assert; 8424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 9424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)import org.chromium.chrome.browser.util.StreamUtil; 10424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 11424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)import java.io.IOException; 12424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)import java.io.InputStream; 13424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)import java.net.MalformedURLException; 14424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)import java.net.URL; 15424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 16424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)/** 17424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) * A test utility class to get URLs that point to the test HTTP server, and to verify that it is up 18424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) * and giving expected responses to given URLs. 19424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) */ 20424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)public final class TestHttpServerClient { 21424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) private static final int SERVER_PORT = 8000; 22424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 23424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) private TestHttpServerClient() { 24424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 25424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 26424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) /** 27424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) * Construct a suitable URL for loading a test data file from the hosts' HTTP server. 28424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) * 29424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) * @param path path relative to the document root. 30424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) * @return an HTTP url. 31424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) */ 32424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) public static String getUrl(String path) { 33424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return "http://localhost:" + SERVER_PORT + "/" + path; 34424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 35424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 36424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) /** 37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * Construct a URL for loading a test data file with HTTP authentication fields. 38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * 39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * @param path path relative to the document root. 40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * @return an HTTP url. 41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) */ 42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public static String getUrl(String path, String username, String password) { 43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return "http://" + username + ":" + password + "@localhost:" + SERVER_PORT + "/" + path; 44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) /** 47424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) * Establishes a connection with the test server at default URL and verifies that it is running. 48424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) */ 49424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) public static void checkServerIsUp() { 50424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) checkServerIsUp(getUrl("chrome/test/data/android/ok.txt"), "OK Computer"); 51424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 52424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 53424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) /** 54424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) * Establishes a connection with the test server at a given URL and verifies that it is running 55424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) * by making sure that the expected response is received. 56424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) */ 57424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) public static void checkServerIsUp(String serverUrl, String expectedResponse) { 58424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) InputStream is = null; 59424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) try { 60424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) URL testUrl = new URL(serverUrl); 61424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) is = testUrl.openStream(); 62424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) byte[] buffer = new byte[128]; 63424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) int length = is.read(buffer); 64424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) Assert.assertNotSame("Failed to access test HTTP Server at URL: " + serverUrl, 65424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) -1, expectedResponse.length()); 66424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) Assert.assertEquals("Failed to access test HTTP Server at URL: " + serverUrl, 67424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) expectedResponse, new String(buffer, 0, length).trim()); 68424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } catch (MalformedURLException e) { 69424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) Assert.fail( 70424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) "Failed to check test HTTP server at URL: " + serverUrl + ". Status: " + e); 71424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } catch (IOException e) { 72424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) Assert.fail( 73424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) "Failed to check test HTTP server at URL: " + serverUrl + ". Status: " + e); 74424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } finally { 75424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) StreamUtil.closeQuietly(is); 76424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 77424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 78424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 79