@Test(expectedExceptions = InvalidBindingException.class)
  public final void bindRequestDetectInvalid() {
    final HttpServletRequest request = createMock(HttpServletRequest.class);

    expect(request.getParameterMap())
        .andReturn(
            new HashMap<String, String[]>() {
              {
                put("name.toString()", new String[] {"Dhanji"});
                put("2 + 12", new String[] {"27"});
                put("#@!*^&", new String[] {"true"});
                put("id", new String[] {"12"});
                put("height", new String[] {"6.0"});
              }
            });

    replay(request);

    final AnObject o = new AnObject();

    final Evaluator evaluator = Guice.createInjector().getInstance(Evaluator.class);

    new MvelRequestBinder(
            evaluator,
            new Provider<FlashCache>() {
              public FlashCache get() {
                return new HttpSessionFlashCache();
              }
            })
        .bind(TestRequestCreator.from(request, null), o);
  }
  @Test
  public final void bindRequestToPrimitivesAndIgnoreExtras() {
    final HttpServletRequest request = createMock(HttpServletRequest.class);

    expect(request.getParameterMap())
        .andReturn(
            new HashMap<String, String[]>() {
              {
                put("name", new String[] {"Dhanji"});
                put("age", new String[] {"27"});
                put("alive", new String[] {"true"});
                put("id", new String[] {"12"});
                put("height", new String[] {"6.0"});
                put("weight", new String[] {"6.0"});
                put("hiphop", new String[] {"6.0"});
              }
            });

    replay(request);

    final AnObject o = new AnObject();

    final Evaluator evaluator = Guice.createInjector().getInstance(Evaluator.class);

    new MvelRequestBinder(
            evaluator,
            new Provider<FlashCache>() {
              public FlashCache get() {
                return new HttpSessionFlashCache();
              }
            })
        .bind(TestRequestCreator.from(request, null), o);

    assert "Dhanji".equals(o.getName());
    assert 27 == (o.getAge());
    assert 12L == (o.getId());
    assert 6.0 == (o.getHeight());
    assert (o.isAlive());

    verify(request);
  }
  @Test
  public final void bindRequestToCollections() {
    final HttpServletRequest request = createMock(HttpServletRequest.class);
    final String choice = "AChoice";

    // setup preliminary request
    final HttpSessionFlashCache cache = new HttpSessionFlashCache();
    cache.put("names", Arrays.asList("First", choice, "BobLee", "JasonLee", "Mowglee"));

    expect(request.getParameterMap())
        .andReturn(
            new HashMap<String, String[]>() {
              {
                put(
                    "select",
                    new String[] {
                      RequestBinder.COLLECTION_BIND_PREFIX + "names/" + choice.hashCode()
                    });
              }
            });

    replay(request);

    final AnObject o = new AnObject();

    final Evaluator evaluator = Guice.createInjector().getInstance(Evaluator.class);

    new MvelRequestBinder(
            evaluator,
            new Provider<FlashCache>() {
              public FlashCache get() {
                return cache;
              }
            })
        .bind(TestRequestCreator.from(request, null), o);

    assert choice.equals(o.getSelect()) : "Collection selectee was not bound: " + o.getSelect();
    verify(request);
  }
예제 #4
0
  @Test(dataProvider = HEADERS_AND_NEGOTIATIONS)
  public final void variousHeadersAndNegotiations(
      Map<String, String> negotiations,
      final Multimap<String, String> headers,
      boolean shouldPass) {
    HttpServletRequest request =
        new HttpServletRequestWrapper(createMock(HttpServletRequest.class)) {
          @Override
          public Enumeration getHeaders(String name) {
            return Iterators.asEnumeration(headers.get(name).iterator());
          }

          @Override
          public Enumeration getHeaderNames() {
            return Iterators.asEnumeration(headers.keys().iterator());
          }
        };

    assert shouldPass
        == new WildcardNegotiator()
            .shouldCall(negotiations, TestRequestCreator.from(request, null));
  }