View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.http.header;
18  
19  import static java.time.format.DateTimeFormatter.*;
20  import static java.time.temporal.ChronoUnit.*;
21  import static org.apache.juneau.TestUtils.*;
22  import static org.apache.juneau.http.HttpHeaders.*;
23  import static org.junit.jupiter.api.Assertions.*;
24  
25  import java.io.*;
26  import java.time.*;
27  import java.util.function.*;
28  
29  import org.apache.juneau.*;
30  import org.apache.juneau.annotation.*;
31  import org.apache.juneau.common.utils.*;
32  import org.apache.juneau.http.annotation.*;
33  import org.apache.juneau.rest.annotation.*;
34  import org.apache.juneau.rest.client.*;
35  import org.apache.juneau.rest.mock.*;
36  import org.junit.jupiter.api.*;
37  
38  class RetryAfter_Test extends TestBase {
39  
40  	private static final String HEADER = "Retry-After";
41  	private static final String VALUE1 = "123";
42  	private static final String VALUE2 = "Sat, 29 Oct 1994 19:43:31 GMT";
43  	private static final Integer PARSED1 = 123;
44  	private static final ZonedDateTime PARSED2 = ZonedDateTime.from(RFC_1123_DATE_TIME.parse(VALUE2)).truncatedTo(SECONDS);
45  
46  	@Rest
47  	public static class A {
48  		@RestOp
49  		public StringReader get(@Header(name=HEADER) @Schema(cf="multi") String[] h) {
50  			return reader(h == null ? "null" : Utils.join(h, ','));
51  		}
52  	}
53  
54  	//------------------------------------------------------------------------------------------------------------------
55  	// Method tests
56  	//------------------------------------------------------------------------------------------------------------------
57  
58  	@Test void a01_basic() throws Exception {
59  		var c = client().build();
60  
61  		// Normal usage.
62  		c.get().header(retryAfter(VALUE1)).run().assertContent(VALUE1);
63  		c.get().header(retryAfter(VALUE1)).run().assertContent(VALUE1);
64  		c.get().header(retryAfter(PARSED1)).run().assertContent(VALUE1);
65  		c.get().header(retryAfter(()->PARSED1)).run().assertContent(VALUE1);
66  
67  		c.get().header(retryAfter(VALUE2)).run().assertContent(VALUE2);
68  		c.get().header(retryAfter(VALUE2)).run().assertContent(VALUE2);
69  		c.get().header(retryAfter(PARSED2)).run().assertContent(VALUE2);
70  		c.get().header(retryAfter(()->PARSED2)).run().assertContent(VALUE2);
71  
72  		// Invalid usage.
73  		c.get().header(retryAfter((String)null)).run().assertContent().isEmpty();
74  		c.get().header(retryAfter((ZonedDateTime)null)).run().assertContent().isEmpty();
75  		c.get().header(retryAfter((Supplier<?>)null)).run().assertContent().isEmpty();
76  		c.get().header(retryAfter(()->null)).run().assertContent().isEmpty();
77  	}
78  
79  	@Test void a02_asZonedDateTime() {
80  		assertEquals("1994-10-29T19:43:31Z", retryAfter(PARSED2).asZonedDateTime().get().toString());
81  	}
82  
83  	@Test void a03_asInt() {
84  		assertEquals(123, retryAfter(123).asInteger().get());
85  		assertEmpty(new RetryAfter((String)null).asInteger());
86  		assertEmpty(retryAfter(()->null).asInteger());
87  	}
88  
89  	//------------------------------------------------------------------------------------------------------------------
90  	// Helper methods.
91  	//------------------------------------------------------------------------------------------------------------------
92  
93  	private static RestClient.Builder client() {
94  		return MockRestClient.create(A.class);
95  	}
96  }