Package org.springframework.http

Examples of org.springframework.http.MockHttpInputMessage


  @Test
  public void readTyped() throws IOException {
    String body = "{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," +
        "\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
    inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
    MyBean result = (MyBean) this.converter.read(MyBean.class, inputMessage);

    assertEquals("Foo", result.getString());
    assertEquals(42, result.getNumber());
    assertEquals(42F, result.getFraction(), 0F);
View Full Code Here


  @Test
  @SuppressWarnings("unchecked")
  public void readUntyped() throws IOException {
    String body = "{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," +
        "\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
    inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
    HashMap<String, Object> result = (HashMap<String, Object>) this.converter.read(HashMap.class, inputMessage);
    assertEquals("Foo", result.get("string"));
    Number n = (Number) result.get("number");
    assertEquals(42, n.longValue());
    n = (Number) result.get("fraction");
View Full Code Here

  }

  @Test(expected = HttpMessageNotReadableException.class)
  public void readInvalidJson() throws IOException {
    String body = "FooBar";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
    inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
    this.converter.read(MyBean.class, inputMessage);
  }
View Full Code Here

        }
      }
    };
    String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," +
        "\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(
        body.getBytes(UTF8));
    inputMessage.getHeaders().setContentType(new MediaType("application", "json"));

    List<MyBean> results = (List<MyBean>) converter.read(List.class, inputMessage);
    assertEquals(1, results.size());
    MyBean result = results.get(0);
    assertEquals("Foo", result.getString());
View Full Code Here

    ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<List<MyBean>>() {
    };

    String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," +
        "\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(
        body.getBytes(UTF8));
    inputMessage.getHeaders().setContentType(new MediaType("application", "json"));

    GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
    List<MyBean> results = (List<MyBean>) converter.read(beansList.getType(), null, inputMessage);
    assertEquals(1, results.size());
    MyBean result = results.get(0);
View Full Code Here

  }

  @Test
  public void readXmlRootElement() throws Exception {
    byte[] body = "<rootElement><type s=\"Hello World\"/></rootElement>".getBytes("UTF-8");
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
    RootElement result = (RootElement) converter.read(RootElement.class, inputMessage);
    assertEquals("Invalid result", "Hello World", result.type.s);
  }
View Full Code Here

  }

  @Test
  public void readXmlRootElementSubclass() throws Exception {
    byte[] body = "<rootElement><type s=\"Hello World\"/></rootElement>".getBytes("UTF-8");
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
    RootElementSubclass result = (RootElementSubclass) converter.read(RootElementSubclass.class, inputMessage);
    assertEquals("Invalid result", "Hello World", result.getType().s);
  }
View Full Code Here

  }

  @Test
  public void readXmlType() throws Exception {
    byte[] body = "<foo s=\"Hello World\"/>".getBytes("UTF-8");
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
    Type result = (Type) converter.read(Type.class, inputMessage);
    assertEquals("Invalid result", "Hello World", result.s);
  }
View Full Code Here

    Resource external = new ClassPathResource("external.txt", getClass());
    String content =  "<!DOCTYPE root SYSTEM \"http://192.168.28.42/1.jsp\" [" +
        "  <!ELEMENT external ANY >\n" +
        "  <!ENTITY ext SYSTEM \"" + external.getURI() + "\" >]>" +
        "  <rootElement><external>&ext;</external></rootElement>";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
    RootElement rootElement = (RootElement) converter.read(RootElement.class, inputMessage);

    assertEquals("", rootElement.external);
  }
View Full Code Here

    Resource external = new ClassPathResource("external.txt", getClass());
    String content =  "<!DOCTYPE root [" +
        "  <!ELEMENT external ANY >\n" +
        "  <!ENTITY ext SYSTEM \"" + external.getURI() + "\" >]>" +
        "  <rootElement><external>&ext;</external></rootElement>";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
    this.converter.setProcessExternalEntities(true);
    RootElement rootElement = (RootElement) converter.read(RootElement.class, inputMessage);

    assertEquals("Foo Bar", rootElement.external);
  }
View Full Code Here

TOP

Related Classes of org.springframework.http.MockHttpInputMessage

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.