]> git.basschouten.com Git - openhab-addons.git/blob
d7ed594b31ac0e694eb1c2f9e6178a33e839d010
[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.webservice;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.*;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jetty.client.api.Response;
20 import org.eclipse.jetty.http.HttpFields;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.mielecloud.internal.webservice.exception.TooManyRequestsException;
23
24 /**
25  * @author Björn Lange - Initial contribution
26  */
27 @NonNullByDefault
28 public class HttpUtilTest {
29     @Test
30     public void whenTheResponseHasARetryAfterHeaderThenItIsParsedAndPassedWithTheException() {
31         // given:
32         HttpFields httpFields = mock(HttpFields.class);
33         when(httpFields.containsKey("Retry-After")).thenReturn(true);
34         when(httpFields.get("Retry-After")).thenReturn("100");
35
36         Response response = mock(Response.class);
37         when(response.getStatus()).thenReturn(429);
38         when(response.getReason()).thenReturn("Too many requests!");
39         when(response.getHeaders()).thenReturn(httpFields);
40
41         // when:
42         try {
43             HttpUtil.checkHttpSuccess(response);
44             fail();
45         } catch (TooManyRequestsException e) {
46             // then:
47             assertEquals(100L, e.getSecondsUntilRetry());
48         }
49     }
50 }