bit.changepurse.wdk.http.TestHTTPRequest.java Source code

Java tutorial

Introduction

Here is the source code for bit.changepurse.wdk.http.TestHTTPRequest.java

Source

/*
 * This file is part of ChangePurse.
 *
 * Copyright (c) 2014 Germn Fuentes Capella
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package bit.changepurse.wdk.http;

import static bit.changepurse.wdk.util.CheckedExceptionMethods.newURI;
import static bit.changepurse.wdk.util.CheckedExceptionMethods.newURL;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.isIn;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;

import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.ParseException;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpTrace;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

public class TestHTTPRequest {

    private static final String TEST_URL = "http://www.google.com";
    private static final Map<HTTPMethod, Class<? extends HttpRequestBase>> mapping;

    static {
        Map<HTTPMethod, Class<? extends HttpRequestBase>> tmpMapping = new HashMap<HTTPMethod, Class<? extends HttpRequestBase>>();
        tmpMapping.put(HTTPMethod.GET, HttpGet.class);
        tmpMapping.put(HTTPMethod.HEAD, HttpHead.class);
        tmpMapping.put(HTTPMethod.POST, HttpPost.class);
        tmpMapping.put(HTTPMethod.PUT, HttpPut.class);
        tmpMapping.put(HTTPMethod.DELETE, HttpDelete.class);
        tmpMapping.put(HTTPMethod.TRACE, HttpTrace.class);
        mapping = Collections.unmodifiableMap(tmpMapping);
    }

    @Test
    public void testBuild() {
        for (HTTPMethod method : HTTPMethod.values()) {
            try {
                HTTPRequest request = new HTTPRequest();
                request.setMethod(method);
                request.setUrl(TEST_URL);
                HttpRequestBase apacheRequest = request.build();
                assertThat(apacheRequest, instanceOf(mapping.get(method)));
                assertThat(apacheRequest.getURI(), equalTo(newURI(TEST_URL)));
            } catch (HTTPException exception) {
                assertThat(method, not(isIn(EnumSet.of(HTTPMethod.GET, HTTPMethod.POST))));
            }
        }
    }

    @Test
    public void testParameters() throws ParseException, IOException {
        URL testUrl = newURL(TEST_URL);
        String key = "key";
        String value = "value";
        HTTPRequest request = new HTTPRequest().setMethod(HTTPMethod.POST).setUrl(testUrl).addParameter(key, value);

        HttpRequestBase apacheRequest = request.build();

        assertThat(apacheRequest, instanceOf(HttpPost.class));
        assertThat(apacheRequest.getURI(), equalTo(newURI(testUrl)));
        String entity = EntityUtils.toString(((HttpPost) apacheRequest).getEntity());
        assertThat(entity, containsString(key + "=" + value));
    }
}