]> git.basschouten.com Git - openhab-addons.git/blob
f07742f307f83a82d48fec469ce3b54411bbed3a
[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.eclipse.jetty.http.HttpHeader;
21 import org.openhab.core.io.net.http.HttpClientFactory;
22
23 /**
24  * Allows for requesting website content from URLs.
25  *
26  * @author Björn Lange - Initial Contribution
27  */
28 @NonNullByDefault
29 public final class WebsiteCrawler implements AutoCloseable {
30     private HttpClient httpClient;
31
32     public WebsiteCrawler(HttpClientFactory httpClientFactory) throws Exception {
33         this.httpClient = httpClientFactory.createHttpClient("mielecloud-int-tests");
34         this.httpClient.start();
35     }
36
37     /**
38      * Gets a website during integration tests.
39      * The resulting website will be the one obtained after following all redirections.
40      *
41      * @param url The URL.
42      * @return The website.
43      * @throws Exception if anything goes wrong.
44      */
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());
50     }
51
52     /**
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.
55      *
56      * @param relativeUrl The relative URL.
57      * @return The website.
58      * @throws Exception if anything goes wrong.
59      */
60     public Website doGetRelative(String relativeUrl) throws Exception {
61         return doGet("http://127.0.0.1:" + getServerPort() + relativeUrl);
62     }
63
64     /**
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.
67      *
68      * @param relativeUrl The relative URL.
69      * @return The website that the client was redirected to.
70      * @throws Exception if anything goes wrong.
71      */
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);
77     }
78
79     /**
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.
82      */
83     public static int getServerPort() {
84         return Integer.getInteger("org.osgi.service.http.port", 8080);
85     }
86
87     @Override
88     public void close() throws Exception {
89         httpClient.stop();
90     }
91 }