Skip to content

base package

Federico Dossena edited this page Sep 12, 2019 · 2 revisions
Utils class

This class provides some static functions used throughout the test. These functions are:

  • urlEncode(String s): URL-encodes a string (UTF-8)
  • sleep(long ms) and sleep(long ms, int ns): simple sleep functions
  • url_sep(String url): Returns the proper separator to use for GET parameters for a given string (eg. for "https://example.com/index.php" it will return "?", for "https://example.com/?id=whatever" it will return "&")
Connection class

This is the foundation on which relies most of the test.

It uses a Socket to connect to the HTTP server, using either HTTP or HTTPS, and provides several methods to interact with it.

Constructors:

public Connection(String url, int connectTimeout, int soTimeout, int recvBuffer, int sendBuffer)
  • url is the URL of the host that you want to connect to (eg. "https://speedtest.fdossena.com")
  • connectTimeout and soTimeout are the timeouts for the socket in milliseconds. Set them to -1 to let the system determine them
  • recvBuffer and sendBuffer are sizes of the buffers used by the socket in bytes. Set them to -1 to let the system determine them
public Connection(String url)

If you use this constructor, it will use a connectTimeout of 2000, a soTimeout of 5000 and default buffer sizes.

Note: URLs can be in one of these formats:

  • http://address: if the server only supports HTTP
  • https://address: if the server only supports HTTPS
  • //address: if the server supports both HTTP and HTTPS (HTTPS preferred)

Important: This class DOES NOT handle HTTP redirects (3xx codes)!

Methods:

  • getInputStream(): returns the socket's InputStream or null if the socket is closed/dead
  • getOutputStream(): returns the socket's OutputStream or null if the socket is closed/dead
  • getPrintStream(): similar to getOutputStream but it returns a PrintStream instead, which is more convenient for writing UTF-8 strings
  • getInputStreamReader(): similar to getInputStream but it returns an InputStreamReader instead, which is more convenient for reading UTF-8 strings
  • GET(String path, boolean keepAlive): writes a GET request on the socket to fetch the resource at the requested path. If keepAlive is set to true, it also sends a Connection: keep-alive header so that the Connection can be reused. An Exception is thrown if something goes wrong.
  • POST(String path, boolean keepAlive, String contentType, long contentLength):: writes a POST request on the socket to fetch the requested path after sending some data. An Exception is thrown if something goes wrong.
    • keepAlive: if set to true, it will send the Connection: keep-alive header and the Connection can be reused
    • contentType: if not null, it will send the Content-Type header with the specified content type
    • contentLength: if >=0, it will send the Content-Length header, and more data can be written to the socket's OutputStream
  • readLineUnbuffered(): reads a line from the socket's InputStream until \n is encountered
  • parseResponseHeaders(): reads an entire HTTP response from the socket's InputStream. The headers are returned in a Hashmap<String,String>. Throws an exception if something goes wrong or if the response was not a 200 OK
  • close(): closes the socket
Clone this wiki locally