PageRenderTime 29ms CodeModel.GetById 20ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/sitebricks/src/test/java/com/google/sitebricks/http/negotiate/RegexNegotiatorTest.java

http://github.com/dhanji/sitebricks
Java | 110 lines | 72 code | 27 blank | 11 comment | 1 complexity | 3a22924b302c08a879ecd33bceb95b6b MD5 | raw file
  1package com.google.sitebricks.http.negotiate;
  2
  3import com.google.common.collect.ImmutableMap;
  4import com.google.common.collect.Iterators;
  5import com.google.common.collect.Multimap;
  6import com.google.common.collect.Multimaps;
  7import com.google.sitebricks.TestRequestCreator;
  8import org.testng.annotations.DataProvider;
  9import org.testng.annotations.Test;
 10
 11import javax.servlet.http.HttpServletRequest;
 12import javax.servlet.http.HttpServletRequestWrapper;
 13import java.util.Enumeration;
 14import java.util.Map;
 15
 16import static org.easymock.EasyMock.createMock;
 17
 18public class RegexNegotiatorTest {
 19  private static final String HEADERS_AND_NEGOTIATIONS = "HEADERS_NEGS";
 20
 21  @DataProvider(name = HEADERS_AND_NEGOTIATIONS)
 22  public Object[][] headersAndNegotiations() {
 23    return new Object[][] {
 24        { ImmutableMap.of(), Multimaps.forMap(ImmutableMap.of()), true },
 25
 26        // negotation, but no headers matching
 27        { ImmutableMap.of("Accept", "thing"), Multimaps.forMap(ImmutableMap.of()), false },
 28        { ImmutableMap.of("Accept", "thing, other-thing"), Multimaps.forMap(ImmutableMap.of()), false },
 29
 30        // headers but no negs
 31        { ImmutableMap.of(), Multimaps.forMap(ImmutableMap.of("Accept", "image/png")), true },
 32
 33        // disjoint set of headers and negs
 34        { ImmutableMap.of("Accept", "thing"), Multimaps.forMap(ImmutableMap.of("Content-Accept", "image/png")), false },
 35        { ImmutableMap.of("Accept", ".*thing.*"), Multimaps.forMap(ImmutableMap.of("Content-Accept", "image/png")), false },
 36
 37        // Non matching set of the same header
 38        { ImmutableMap.of("Accept", ".*thing.*"), Multimaps.forMap(ImmutableMap.of("Accept", "image/png")), false },
 39
 40        // Matching set of the same header
 41        { ImmutableMap.of("Accept", ".*thing.*"), Multimaps.forMap(ImmutableMap.of("Accept", "thing")), true },
 42
 43        // Matching set of the same header, but different cases
 44        { ImmutableMap.of("Accept", ".*thing.*"), Multimaps.forMap(ImmutableMap.of("Accept", "THING")), false },
 45
 46        // Multiple header values, one matches
 47        { ImmutableMap.of("Accept", ".*thing.*"), Multimaps.forMap(ImmutableMap.of
 48          ("Accept", "nonthing, thing")), true },
 49        { ImmutableMap.of("Referer", ".*(google|yahoo|bing)\\.com.*"), Multimaps.forMap(ImmutableMap.of
 50          ("Accept", "text/*, nonthing", "Referer", "http://google.com/")), true },
 51        { ImmutableMap.of("Accept", ".*text/.*"), Multimaps.forMap(ImmutableMap.of
 52          ("Accept", "nonthing, text/plain", "Referer", "http://google.com/")), true },
 53
 54        // Multiple header values, one matches, different order
 55        { ImmutableMap.of("Accept", ".*thing.*"), Multimaps.forMap(ImmutableMap.of("Accept", "thing, hno, asdo")), true },
 56
 57        // Multiple header values, some match, some don't
 58        { ImmutableMap.of("Accept", "thing", "Content-Accept", "nothing"), Multimaps.forMap(ImmutableMap.of("Accept", "thing, hno, asdo")), false },
 59
 60        // Multiple header values, some match, some don't, but all passes
 61        { ImmutableMap.of("Accept", ".*thing.*", "Content-Accept", ".*nothing.*"), Multimaps.forMap(ImmutableMap.of("Accept", "thing, hno, asdo", "Content-Accept", "aisdjf, nothing, aiosjdf")), true },
 62
 63
 64
 65
 66        // from the wild
 67        { ImmutableMap.of("Accept", ".*text/(\\*|html).*"), Multimaps.forMap(ImmutableMap.of
 68          ("Accept", "text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5")),
 69          true },
 70        { ImmutableMap.of("Accept", ".*/xml.*"), Multimaps.forMap(ImmutableMap.of
 71          ("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")),
 72          true },
 73        { ImmutableMap.of("Accept", ".*(application|text)/.*xml.*"), Multimaps.forMap(ImmutableMap.of
 74          ("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")),
 75          true },
 76        { ImmutableMap.of("Accept", ".*(application|text)/.*xml.*"), Multimaps.forMap(ImmutableMap.of
 77          ("Accept", "application/xml,application/xhtml+xml,text/html;q=0.9, text/plain;q=0.8,image/png,*/*;q=0.5")),
 78          true },
 79        { ImmutableMap.of("Accept", ".*application/.*(silverlight|flash|shockwave).*"), Multimaps.forMap(ImmutableMap.of
 80        ("Accept", "image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/msword, " +
 81          "application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-shockwave-flash, " +
 82          "application/x-silverlight-2-b2, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*")),
 83        true },
 84
 85
 86
 87
 88    };
 89  }
 90
 91
 92  @Test(dataProvider = HEADERS_AND_NEGOTIATIONS)
 93  public final void variousHeadersAndNegotiations(Map<String, String> negotiations,
 94                                                  final Multimap<String, String> headers,
 95                                                  boolean shouldPass) {
 96    HttpServletRequest request = new HttpServletRequestWrapper(createMock(HttpServletRequest.class)) {
 97      @Override
 98      public Enumeration getHeaders(String name) {
 99        return Iterators.asEnumeration(headers.get(name).iterator());
100      }
101
102      @Override public Enumeration getHeaderNames() {
103        return Iterators.asEnumeration(headers.keys().iterator());
104      }
105    };
106
107    assert shouldPass == new RegexNegotiator().shouldCall(negotiations, TestRequestCreator.from(
108        request, null));
109  }
110}