/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

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