]> git.basschouten.com Git - openhab-addons.git/blob
45d62deac20ad088574f91f0b3500d054cc2af40
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mielecloud.internal.util;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16
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.openhab.core.io.net.http.HttpClientFactory;
21
22 /**
23  * Allows for requesting website content from URLs.
24  *
25  * @author Björn Lange - Initial Contribution
26  */
27 @NonNullByDefault
28 public final class WebsiteCrawler {
29     private HttpClient httpClient;
30
31     public WebsiteCrawler(HttpClientFactory httpClientFactory) {
32         this.httpClient = httpClientFactory.getCommonHttpClient();
33     }
34
35     /**
36      * Gets a website relative to the address of the openHAB installation running in test mode during integration tests.
37      *
38      * @param relativeUrl The relative URL.
39      * @return The website.
40      * @throws Exception if anything goes wrong.
41      */
42     public Website doGetRelative(String relativeUrl) throws Exception {
43         ContentResponse response = httpClient.GET("http://127.0.0.1:" + getServerPort() + relativeUrl);
44         assertEquals(200, response.getStatus());
45         return new Website(response.getContentAsString());
46     }
47
48     /**
49      * Gets the port the webserver for this integration test instance is running on. The port is reserved in the pom.xml
50      * by the build-helper-maven-plugin.
51      */
52     public static int getServerPort() {
53         return Integer.getInteger("org.osgi.service.http.port", 8080);
54     }
55 }