This project has retired. For details please refer to its Attic page.
AcceptHeaderBuilderTest xref
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  
18  package org.apache.any23.http;
19  
20  import org.apache.any23.mime.MIMEType;
21  import org.junit.Assert;
22  import org.junit.Test;
23  
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.Collection;
27  import java.util.Collections;
28  
29  /**
30   * Reference test for {@link AcceptHeaderBuilder}
31   * 
32   */
33  public class AcceptHeaderBuilderTest {
34  
35      @Test
36      public void testEmpty() {
37          Assert.assertNull(buildHeader(Collections.<String> emptyList()));
38      }
39  
40      @Test
41      public void testSingleHeaderSpecific() {
42          Assert.assertEquals("text/html", buildHeader(Arrays.asList("text/html")));
43      }
44  
45      @Test
46      public void testSingleHeaderSpecificWithQ() {
47          Assert.assertEquals("text/html;q=0.5", buildHeader(Arrays.asList("text/html;q=0.5")));
48      }
49  
50      @Test
51      public void testSuppressQIfEquals1() {
52          Assert.assertEquals("text/html", buildHeader(Arrays.asList("text/html;q=1")));
53      }
54  
55      @Test
56      public void testSingleHeaderSubtypeWildcard() {
57          Assert.assertEquals("text/*;q=0.5", buildHeader(Arrays.asList("text/*;q=0.5")));
58      }
59  
60      @Test
61      public void testSingleHeaderTypeWildcard() {
62          Assert.assertEquals("*/*;q=0.5", buildHeader(Arrays.asList("*/*;q=0.5")));
63      }
64  
65      @Test
66      public void testMultipleIndependentHeaders() {
67          Assert.assertEquals("image/jpeg;q=0.2, text/html, text/plain;q=0.5",
68                  buildHeader(Arrays.asList("image/jpeg;q=0.2", "text/html;q=1.0", "text/plain;q=0.5")));
69      }
70  
71      @Test
72      public void testHighestSpecificValueIsChosen() {
73          Assert.assertEquals("image/jpeg", buildHeader(Arrays.asList("image/jpeg;q=0.2", "image/jpeg")));
74          Assert.assertEquals("image/jpeg", buildHeader(Arrays.asList("image/jpeg", "image/jpeg;q=0.2")));
75      }
76  
77      @Test
78      public void testHighestSubtypeWildcardIsChosen() {
79          Assert.assertEquals("image/*", buildHeader(Arrays.asList("image/*;q=0.2", "image/*")));
80          Assert.assertEquals("image/*", buildHeader(Arrays.asList("image/*", "image/*;q=0.2")));
81      }
82  
83      @Test
84      public void testHighestTypeWildcardIsChosen() {
85          Assert.assertEquals("*/*", buildHeader(Arrays.asList("*/*;q=0.2", "*/*")));
86          Assert.assertEquals("*/*", buildHeader(Arrays.asList("*/*", "*/*;q=0.2")));
87      }
88  
89      @Test
90      public void testTypeWildcardSuppressesLowerValues() {
91          Assert.assertEquals("*/*;q=0.5", buildHeader(Arrays.asList("*/*;q=0.5", "image/*;q=0.2")));
92          Assert.assertEquals("*/*;q=0.5", buildHeader(Arrays.asList("*/*;q=0.5", "image/jpeg;q=0.2")));
93      }
94  
95      @Test
96      public void testSubtypeWildcardSuppressesLowerValues() {
97          Assert.assertEquals("image/*;q=0.5", buildHeader(Arrays.asList("image/*;q=0.5", "image/jpeg;q=0.2")));
98      }
99  
100     private String buildHeader(Collection<String> mimeTypes) {
101         Collection<MIMEType> parsedTypes = new ArrayList<MIMEType>();
102         for (String s : mimeTypes) {
103             parsedTypes.add(MIMEType.parse(s));
104         }
105         return new AcceptHeaderBuilder(parsedTypes).getAcceptHeader();
106     }
107 }