Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
  • Loading branch information
J-N-K committed Apr 27, 2021
1 parent d4c2a57 commit da173d7
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Copyright (c) 2021 Contributors to the SmartHome/J project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.smarthomej.binding.http;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.removeAllMappings;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.net.URI;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.http.HttpMethod;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.openhab.core.test.TestPortUtil;
import org.smarthomej.binding.http.internal.http.RateLimitedHttpClient;

import com.github.tomakehurst.wiremock.WireMockServer;

@NonNullByDefault
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class RateLimitedHttpClientTest {

private int port = 0;
private @NonNullByDefault({}) WireMockServer wireMockServer;
private @NonNullByDefault({}) HttpClient httpClient;
private ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(4);

@BeforeAll
public void init() throws Exception {
port = TestPortUtil.findFreePort();

wireMockServer = new WireMockServer(port);
wireMockServer.start();

httpClient = new HttpClient();
httpClient.start();

configureFor("localhost", port);
}

@AfterAll
public void close() throws Exception {
wireMockServer.shutdown();
scheduler.shutdown();
httpClient.stop();
}

@Test
public void simpleHttpTest() throws Exception {
String testLocation = "/testlocation";
String testResponse = "Test response";
stubFor(get(urlEqualTo(testLocation)).willReturn(aResponse().withBody(testResponse)));

RateLimitedHttpClient rateLimitedHttpClient = new RateLimitedHttpClient(httpClient, scheduler);
rateLimitedHttpClient.setDelay(0);

URI url = URI.create("http://localhost:" + port + testLocation);
ContentResponse cr = rateLimitedHttpClient.newRequest(url, HttpMethod.GET, "", null).get().send();

assertEquals(testResponse, cr.getContentAsString());

removeAllMappings();
}
}

This file was deleted.

0 comments on commit da173d7

Please sign in to comment.