com.playonlinux.core.webservice.HTTPDownloaderTest.java Source code

Java tutorial

Introduction

Here is the source code for com.playonlinux.core.webservice.HTTPDownloaderTest.java

Source

/*
 * Copyright (C) 2015 PRIS Quentin
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package com.playonlinux.core.webservice;

import static org.junit.Assert.assertEquals;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;

import java.io.File;
import java.io.FileReader;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.io.IOUtils;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.model.Header;

public class HTTPDownloaderTest {
    private static URL mockServerURL;
    private static URL mockServerURLFile2;

    private static ClientAndServer mockServer;
    private static int MOCKSERVER_PORT = 3343;

    @BeforeClass
    public static void setUp() throws MalformedURLException {
        mockServer = new ClientAndServer(MOCKSERVER_PORT);
        mockServerURL = new URL("http://localhost:" + MOCKSERVER_PORT + "/test.txt");
        mockServerURLFile2 = new URL("http://localhost:" + MOCKSERVER_PORT + "/test2.txt");
    }

    @Test
    public void testGet_DownloadFile_FileIsDownloaded() throws Exception {
        mockServer.when(request().withMethod("GET").withPath("/test.txt")).respond(
                response().withStatusCode(200).withHeaders(new Header("Content-Type", "application/config"))
                        .withBody("Content file to download"));

        File temporaryFile = File.createTempFile("test", "txt");
        temporaryFile.deleteOnExit();
        new HTTPDownloader(mockServerURL).get(temporaryFile);

        String fileContent = IOUtils.toString(new FileReader(temporaryFile));

        assertEquals("Content file to download", fileContent);
    }

    @Test
    public void testGet_DownloadFileInAString_FileIsDownloaded() throws Exception {
        mockServer.when(request().withMethod("GET").withPath("/test2.txt")).respond(
                response().withStatusCode(200).withHeaders(new Header("Content-Type", "application/config"))
                        .withBody("Content file to download 2"));

        String result = new HTTPDownloader(mockServerURLFile2).get();

        assertEquals("Content file to download 2", result);
    }

    @AfterClass
    public static void tearDown() {
        mockServer.stop();
    }
}