-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
- Loading branch information
Showing
2 changed files
with
85 additions
and
50 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
...mej.binding.http/src/test/java/org/smarthomej/binding/http/RateLimitedHttpClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
50 changes: 0 additions & 50 deletions
50
...arthomej.binding.http/src/test/org/smarthomej/binding/http/RateLimitedHttpClientTest.java
This file was deleted.
Oops, something went wrong.