/** * Set the list of acceptable {@linkplain MediaType media types}, * as specified by the {@code Accept} header. */ public void setAccept(List<MediaType> acceptableMediaTypes) { set(ACCEPT, MediaType.toString(acceptableMediaTypes)); }
/** * Set the 'Content-Type' header of the request. * @param contentType the content type */ public MockHttpServletRequestBuilder contentType(MediaType contentType) { Assert.notNull(contentType, "'contentType' must not be null"); this.contentType = contentType.toString(); return this; }
@Override public String toString() { StringBuilder builder = new StringBuilder(); if (this.isNegated) { builder.append('!'); } builder.append(this.mediaType.toString()); return builder.toString(); }
@Override @Nullable public String getContentType() { MediaType contentType = this.headers.getContentType(); return (contentType != null ? contentType.toString() : null); }
@Override @Nullable public Header getContentType() { MediaType contentType = this.headers.getContentType(); return (contentType != null ? new BasicHeader("Content-Type", contentType.toString()) : null); }
@Override public void accept(Visitor visitor) { visitor.header(HttpHeaders.ACCEPT, (this.mediaTypes.size() == 1) ? this.mediaTypes.iterator().next().toString() : this.mediaTypes.toString()); }
/** * Set the 'Accept' header to the given media type(s). * @param mediaTypes one or more media types */ public MockHttpServletRequestBuilder accept(MediaType... mediaTypes) { Assert.notEmpty(mediaTypes, "'mediaTypes' must not be empty"); this.headers.set("Accept", MediaType.toString(Arrays.asList(mediaTypes))); return this; }
@Override @Nullable public String getContentType() { MediaType contentType = this.headers.getContentType(); return (contentType != null ? contentType.toString() : null); }
@Test public void resolveMediaTypes() throws Exception { String header = "text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"; List<MediaType> mediaTypes = this.resolver.resolveMediaTypes( MockServerWebExchange.from(MockServerHttpRequest.get("/").header("accept", header))); assertEquals(4, mediaTypes.size()); assertEquals("text/html", mediaTypes.get(0).toString()); assertEquals("text/x-c", mediaTypes.get(1).toString()); assertEquals("text/x-dvi;q=0.8", mediaTypes.get(2).toString()); assertEquals("text/plain;q=0.5", mediaTypes.get(3).toString()); }
@Test public void acceptHeader() { this.builder.accept(MediaType.TEXT_HTML, MediaType.APPLICATION_XML); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); List<String> accept = Collections.list(request.getHeaders("Accept")); List<MediaType> result = MediaType.parseMediaTypes(accept.get(0)); assertEquals(1, accept.size()); assertEquals("text/html", result.get(0).toString()); assertEquals("application/xml", result.get(1).toString()); }
@Test public void resolveRequestMappingViaComposedAnnotation() throws Exception { RequestMappingInfo info = assertComposedAnnotationMapping("postJson", "/postJson", RequestMethod.POST); assertEquals(MediaType.APPLICATION_JSON_VALUE, info.getConsumesCondition().getConsumableMediaTypes().iterator().next().toString()); assertEquals(MediaType.APPLICATION_JSON_VALUE, info.getProducesCondition().getProducibleMediaTypes().iterator().next().toString()); }
@Test public void resolveMediaTypes() throws Exception { Map<String, MediaType> mapping = Collections.singletonMap("json", MediaType.APPLICATION_JSON); TestMappingContentNegotiationStrategy strategy = new TestMappingContentNegotiationStrategy("json", mapping); List<MediaType> mediaTypes = strategy.resolveMediaTypes(null); assertEquals(1, mediaTypes.size()); assertEquals("application/json", mediaTypes.get(0).toString()); }
private ServerHttpRequest generateErrorMultipartRequest() { return MockServerHttpRequest.post("/") .header(CONTENT_TYPE, MULTIPART_FORM_DATA.toString()) .body(Flux.just(new DefaultDataBufferFactory().wrap("invalid content".getBytes()))); }
private void testResolveArgumentWithValidation(SimpleBean simpleBean) throws Exception { MediaType contentType = MediaType.TEXT_PLAIN; servletRequest.addHeader("Content-Type", contentType.toString()); servletRequest.setContent("payload".getBytes(StandardCharsets.UTF_8)); @SuppressWarnings("unchecked") HttpMessageConverter<SimpleBean> beanConverter = mock(HttpMessageConverter.class); given(beanConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN)); given(beanConverter.canRead(SimpleBean.class, contentType)).willReturn(true); given(beanConverter.read(eq(SimpleBean.class), isA(HttpInputMessage.class))).willReturn(simpleBean); processor = new RequestResponseBodyMethodProcessor(Collections.singletonList(beanConverter)); processor.resolveArgument(paramValidBean, mavContainer, webRequest, new ValidatingBinderFactory()); }
@Test public void shouldFailResolvingWhenConverterCannotRead() throws Exception { MediaType contentType = TEXT_PLAIN; servletRequest.setMethod("POST"); servletRequest.addHeader("Content-Type", contentType.toString()); given(stringHttpMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(contentType)); given(stringHttpMessageConverter.canRead(String.class, contentType)).willReturn(false); this.thrown.expect(HttpMediaTypeNotSupportedException.class); processor.resolveArgument(paramHttpEntity, mavContainer, webRequest, null); }
@Test public void handleRequestXhrStreaming() throws Exception { XhrStreamingTransportHandler transportHandler = new XhrStreamingTransportHandler(); transportHandler.initialize(this.sockJsConfig); AbstractSockJsSession session = transportHandler.createSession("1", this.webSocketHandler, null); transportHandler.handleRequest(this.request, this.response, this.webSocketHandler, session); assertEquals("application/javascript;charset=UTF-8", this.response.getHeaders().getContentType().toString()); assertTrue("Streaming request not started", this.servletRequest.isAsyncStarted()); verify(this.webSocketHandler).afterConnectionEstablished(session); }
@Test(expected = HttpMediaTypeNotAcceptableException.class) public void handleReturnValueNotAcceptableProduces() throws Exception { MediaType accepted = MediaType.TEXT_PLAIN; servletRequest.addHeader("Accept", accepted.toString()); given(stringMessageConverter.canWrite(String.class, null)).willReturn(true); given(stringMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN)); given(stringMessageConverter.canWrite(String.class, accepted)).willReturn(false); processor.handleReturnValue("Foo", returnTypeStringProduces, mavContainer, webRequest); }
private void testEmitterContentType(String expected) throws Exception { ServletServerHttpResponse message = new ServletServerHttpResponse(this.servletResponse); ResponseBodyEmitter emitter = handleValue(Flux.empty(), Flux.class, forClass(String.class)); emitter.extendResponse(message); assertEquals(expected, message.getHeaders().getContentType().toString()); resetRequest(); }
@Test public void accept() { MediaType json = MediaType.APPLICATION_JSON; RequestPredicate predicate = RequestPredicates.accept(json); MockServerRequest request = MockServerRequest.builder().header("Accept", json.toString()).build(); assertTrue(predicate.test(request)); request = MockServerRequest.builder().header("Accept", MediaType.TEXT_XML_VALUE).build(); assertFalse(predicate.test(request)); }
private void handleRequest(AbstractHttpReceivingTransportHandler transportHandler) throws Exception { WebSocketHandler wsHandler = mock(WebSocketHandler.class); AbstractSockJsSession session = new TestHttpSockJsSession("1", new StubSockJsServiceConfig(), wsHandler, null); transportHandler.initialize(new StubSockJsServiceConfig()); transportHandler.handleRequest(this.request, this.response, wsHandler, session); assertEquals("text/plain;charset=UTF-8", this.response.getHeaders().getContentType().toString()); verify(wsHandler).handleMessage(session, new TextMessage("x")); }