2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mielecloud.internal.util;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jetty.client.HttpClient;
19 import org.eclipse.jetty.client.api.ContentResponse;
20 import org.eclipse.jetty.http.HttpHeader;
21 import org.openhab.core.io.net.http.HttpClientFactory;
24 * Allows for requesting website content from URLs.
26 * @author Björn Lange - Initial Contribution
29 public final class WebsiteCrawler implements AutoCloseable {
30 private HttpClient httpClient;
32 public WebsiteCrawler(HttpClientFactory httpClientFactory) throws Exception {
33 this.httpClient = httpClientFactory.createHttpClient("mielecloud-int-tests");
34 this.httpClient.start();
38 * Gets a website during integration tests.
39 * The resulting website will be the one obtained after following all redirections.
42 * @return The website.
43 * @throws Exception if anything goes wrong.
45 public Website doGet(String url) throws Exception {
46 httpClient.setFollowRedirects(true);
47 ContentResponse response = httpClient.GET(url);
48 assertEquals(200, response.getStatus());
49 return new Website(response.getContentAsString());
53 * Gets a website relative to the address of the openHAB installation running in test mode during integration tests.
54 * The resulting website will be the one obtained after following all redirections.
56 * @param relativeUrl The relative URL.
57 * @return The website.
58 * @throws Exception if anything goes wrong.
60 public Website doGetRelative(String relativeUrl) throws Exception {
61 return doGet("http://127.0.0.1:" + getServerPort() + relativeUrl);
65 * Gets a redirection URL from an URL relative to the address of the openHAB installation running in test mode
66 * during integration tests expecting to receive a 302 Found response.
68 * @param relativeUrl The relative URL.
69 * @return The website that the client was redirected to.
70 * @throws Exception if anything goes wrong.
72 public String doGetRedirectUrlRelative(String relativeUrl) throws Exception {
73 httpClient.setFollowRedirects(false);
74 ContentResponse response = httpClient.GET("http://127.0.0.1:" + getServerPort() + relativeUrl);
75 assertEquals(302, response.getStatus());
76 return response.getHeaders().get(HttpHeader.LOCATION);
80 * Gets the port the webserver for this integration test instance is running on. The port is reserved in the pom.xml
81 * by the build-helper-maven-plugin.
83 public static int getServerPort() {
84 return Integer.getInteger("org.osgi.service.http.port", 8080);
88 public void close() throws Exception {