当前位置: 首页>>代码示例>>Java>>正文


Java MediaType.create方法代码示例

本文整理汇总了Java中com.google.common.net.MediaType.create方法的典型用法代码示例。如果您正苦于以下问题:Java MediaType.create方法的具体用法?Java MediaType.create怎么用?Java MediaType.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.common.net.MediaType的用法示例。


在下文中一共展示了MediaType.create方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: corsSupportShouldReturnBadRequestWhenRequestDoesNotSpecifyMethod

import com.google.common.net.MediaType; //导入方法依赖的package包/类
@Test
public void corsSupportShouldReturnBadRequestWhenRequestDoesNotSpecifyMethod() {
    LambdaProxyHandler<Configuration> handlerWithCorsSupport = new TestLambdaProxyHandler(true);
    Collection<String> supportedMethods = singletonList("POST");
    MediaType mediaType1 = MediaType.create("application", "type1");
    MediaType mediaType2 = MediaType.create("application", "type2");
    MediaType mediaType3 = MediaType.create("application", "type3");
    MediaType mediaType4 = MediaType.create("application", "type4");
    Collection<String> requiredHeaders = Stream.of("header1", "header2")
            .map(Util::randomizeCase)
            .collect(toList());
    SampleMethodHandler sampleMethodHandler = new SampleMethodHandler(requiredHeaders);
    sampleMethodHandler.registerPerAccept(mediaType1, mock(AcceptMapper.class));
    sampleMethodHandler.registerPerAccept(mediaType2, mock(AcceptMapper.class));
    sampleMethodHandler.registerPerAccept(mediaType3, mock(AcceptMapper.class));
    sampleMethodHandler.registerPerContentType(mediaType4, mock(ContentTypeMapper.class));
    supportedMethods.forEach(method -> handlerWithCorsSupport.registerMethodHandler(
            method,
            c -> sampleMethodHandler
    ));
    Map<String, String> headers = new ConcurrentHashMap<>();
    Collection<String> requestHeaders = requiredHeaders.stream()
            .map(Util::randomizeCase)
            .collect(toSet());
    requestHeaders.add("header3");
    headers.put(ACCESS_CONTROL_REQUEST_HEADERS, String.join(", ", requestHeaders));
    headers.put("Content-Type", mediaType4.toString());
    headers.put("Origin", "http://127.0.0.1:8888");
    randomiseKeyValues(headers);
    ApiGatewayProxyRequest request = new ApiGatewayProxyRequestBuilder()
            .withHttpMethod("OPTIONS")
            .withHeaders(headers)
            .withContext(context)
            .build();

    ApiGatewayProxyResponse response = handlerWithCorsSupport.handleRequest(request, context);

    assertThat(response.getStatusCode()).isEqualTo(BAD_REQUEST.getStatusCode());
    assertThat(response.getBody()).contains(String.format("Options method should include the %s header", ACCESS_CONTROL_REQUEST_METHOD.toLowerCase()));
}
 
开发者ID:gabrielle-anderson,项目名称:aws-lambda-proxy-java,代码行数:41,代码来源:LambdaProxyHandlerTest.java

示例2: corsSupportShouldReturnBadRequestForNonRegisteredMethod

import com.google.common.net.MediaType; //导入方法依赖的package包/类
@Test
public void corsSupportShouldReturnBadRequestForNonRegisteredMethod() {
    LambdaProxyHandler<Configuration> handlerWithCORSSupport = new TestLambdaProxyHandler(true);
    String methodBeingInvestigated = "GET";
    Collection<String> supportedMethods = singletonList("POST");
    MediaType mediaType1 = MediaType.create("application", "type1");
    MediaType mediaType2 = MediaType.create("application", "type2");
    MediaType mediaType3 = MediaType.create("application", "type3");
    MediaType mediaType4 = MediaType.create("application", "type4");
    Collection<String> requiredHeaders = Stream.of("header1", "header2")
            .map(Util::randomizeCase)
            .collect(toList());
    SampleMethodHandler sampleMethodHandler = new SampleMethodHandler(requiredHeaders);
    sampleMethodHandler.registerPerAccept(mediaType1, mock(AcceptMapper.class));
    sampleMethodHandler.registerPerAccept(mediaType2, mock(AcceptMapper.class));
    sampleMethodHandler.registerPerAccept(mediaType3, mock(AcceptMapper.class));
    sampleMethodHandler.registerPerContentType(mediaType4, mock(ContentTypeMapper.class));
    supportedMethods.forEach(method -> handlerWithCORSSupport.registerMethodHandler(
            method,
            c -> sampleMethodHandler
    ));
    Map<String, String> headers = new ConcurrentHashMap<>();
    headers.put(ACCESS_CONTROL_REQUEST_METHOD, methodBeingInvestigated);
    Collection<String> requestHeaders = new HashSet<>(requiredHeaders);
    requestHeaders.add("header3");
    headers.put(ACCESS_CONTROL_REQUEST_HEADERS, String.join(", ", requestHeaders));
    headers.put("Content-Type", mediaType4.toString());
    headers.put("Origin", "http://127.0.0.1:8888");
    randomiseKeyValues(headers);
    ApiGatewayProxyRequest request = new ApiGatewayProxyRequestBuilder()
            .withHttpMethod("OPTIONS")
            .withHeaders(headers)
            .withContext(context)
            .build();

    ApiGatewayProxyResponse response = handlerWithCORSSupport.handleRequest(request, context);

    assertThat(response.getStatusCode()).isEqualTo(BAD_REQUEST.getStatusCode());
    assertThat(response.getBody()).isEqualTo(String.format("Lambda cannot handle the method %s", methodBeingInvestigated.toLowerCase()));
}
 
开发者ID:gabrielle-anderson,项目名称:aws-lambda-proxy-java,代码行数:41,代码来源:LambdaProxyHandlerTest.java

示例3: corsSupportShouldReturnBadRequestWhenRequiredHeadersNotPresent

import com.google.common.net.MediaType; //导入方法依赖的package包/类
@Test
public void corsSupportShouldReturnBadRequestWhenRequiredHeadersNotPresent() {
    LambdaProxyHandler<Configuration> handlerWithCORSSupport = new TestLambdaProxyHandler(true);
    String methodBeingInvestigated = "GET";
    Collection<String> supportedMethods = asList(methodBeingInvestigated, "POST");
    MediaType mediaType1 = MediaType.create("application", "type1");
    MediaType mediaType2 = MediaType.create("application", "type2");
    MediaType mediaType3 = MediaType.create("application", "type3");
    MediaType mediaType4 = MediaType.create("application", "type4");
    List<String> requiredHeaders = Stream.of("header1", "header2")
            .map(Util::randomizeCase)
            .collect(toList());
    SampleMethodHandler sampleMethodHandler = new SampleMethodHandler(requiredHeaders);
    sampleMethodHandler.registerPerAccept(mediaType1, mock(AcceptMapper.class));
    sampleMethodHandler.registerPerAccept(mediaType2, mock(AcceptMapper.class));
    sampleMethodHandler.registerPerAccept(mediaType3, mock(AcceptMapper.class));
    sampleMethodHandler.registerPerContentType(mediaType4, mock(ContentTypeMapper.class));
    supportedMethods.forEach(method -> handlerWithCORSSupport.registerMethodHandler(
            method,
            c -> sampleMethodHandler
    ));
    Map<String, String> headers = new ConcurrentHashMap<>();
    headers.put(ACCESS_CONTROL_REQUEST_METHOD, methodBeingInvestigated);
    headers.put(ACCESS_CONTROL_REQUEST_HEADERS, "");
    headers.put("Content-Type", mediaType4.toString());
    headers.put("Origin", "http://127.0.0.1:8888");
    randomiseKeyValues(headers);
    ApiGatewayProxyRequest request = new ApiGatewayProxyRequestBuilder()
            .withHttpMethod("OPTIONS")
            .withHeaders(headers)
            .withContext(context)
            .build();

    ApiGatewayProxyResponse response = handlerWithCORSSupport.handleRequest(request, context);

    assertThat(response.getStatusCode()).isEqualTo(BAD_REQUEST.getStatusCode());
    assertThat(response.getBody()).contains(String.format("The required header(s) not present: %s", String.join(", ", requiredHeaders.stream().map(String::toLowerCase).collect(toList()))));
}
 
开发者ID:gabrielle-anderson,项目名称:aws-lambda-proxy-java,代码行数:39,代码来源:LambdaProxyHandlerTest.java

示例4: corsSupportShouldReturnOkForRegisteredMethodAndMediaTypes

import com.google.common.net.MediaType; //导入方法依赖的package包/类
@Test
public void corsSupportShouldReturnOkForRegisteredMethodAndMediaTypes() {
    LambdaProxyHandler<Configuration> handlerWithCORSSupport = new TestLambdaProxyHandler(true);
    String methodBeingInvestigated = "GET";
    Collection<String> supportedMethods = asList(methodBeingInvestigated, "POST");
    MediaType mediaType1 = MediaType.create("application", "type1");
    MediaType mediaType2 = MediaType.create("application", "type2");
    MediaType mediaType3 = MediaType.create("application", "type3");
    MediaType mediaType4 = MediaType.create("application", "type4");
    Collection<String> requiredHeaders = Stream.of("header1", "header2")
            .map(Util::randomizeCase)
            .collect(toList());
    SampleMethodHandler sampleMethodHandler = new SampleMethodHandler(requiredHeaders);
    sampleMethodHandler.registerPerAccept(mediaType1, mock(AcceptMapper.class));
    sampleMethodHandler.registerPerAccept(mediaType2, mock(AcceptMapper.class));
    sampleMethodHandler.registerPerAccept(mediaType3, mock(AcceptMapper.class));
    sampleMethodHandler.registerPerContentType(mediaType4, mock(ContentTypeMapper.class));
    supportedMethods.forEach(method -> handlerWithCORSSupport.registerMethodHandler(
            method,
            c -> sampleMethodHandler
    ));
    Map<String, String> headers = new ConcurrentHashMap<>();
    headers.put("Access-Control-Request-Method", methodBeingInvestigated);
    Collection<String> requestHeaders = requiredHeaders.stream()
            .map(Util::randomizeCase)
            .collect(toSet());
    requestHeaders.add("header3");
    headers.put("Access-Control-Request-Headers", String.join(", ", requestHeaders));
    headers.put("Content-Type", mediaType4.toString());
    String origin = "http://127.0.0.1:8888";
    headers.put("Origin", origin);
    randomiseKeyValues(headers);
    ApiGatewayProxyRequest request = new ApiGatewayProxyRequestBuilder()
            .withHttpMethod("OPTIONS")
            .withHeaders(headers)
            .withContext(context)
            .build();

    ApiGatewayProxyResponse response = handlerWithCORSSupport.handleRequest(request, context);

    assertThat(response.getStatusCode()).isEqualTo(OK.getStatusCode());
    Map<String, String> responseHeaders = response.getHeaders();
    assertThat(responseHeaders).containsKey("Access-Control-Allow-Origin");
    assertThat(responseHeaders.get("Access-Control-Allow-Origin")).isEqualTo(origin);
    assertThat(responseHeaders).containsKey("Access-Control-Allow-Headers");
    assertThat(asList(responseHeaders.get("Access-Control-Allow-Headers").split(", ")))
            .containsAll(requestHeaders.stream().map(String::toLowerCase).collect(toList()));
    assertThat(responseHeaders).containsKey("Access-Control-Allow-Methods");
    assertThat(responseHeaders.get("Access-Control-Allow-Methods")).isEqualTo(methodBeingInvestigated);
}
 
开发者ID:gabrielle-anderson,项目名称:aws-lambda-proxy-java,代码行数:51,代码来源:LambdaProxyHandlerTest.java

示例5: corsSupportShouldReturnBadRequestWhenRequestDoesNotSpecifyOrigin

import com.google.common.net.MediaType; //导入方法依赖的package包/类
@Test
public void corsSupportShouldReturnBadRequestWhenRequestDoesNotSpecifyOrigin() {
    LambdaProxyHandler<Configuration> handlerWithCORSSupport = new TestLambdaProxyHandler(true);
    String methodBeingInvestigated = "GET";
    Collection<String> supportedMethods = asList(methodBeingInvestigated, "POST");
    MediaType mediaType1 = MediaType.create("application", "type1");
    MediaType mediaType2 = MediaType.create("application", "type2");
    MediaType mediaType3 = MediaType.create("application", "type3");
    MediaType mediaType4 = MediaType.create("application", "type4");
    Collection<String> requiredHeaders = Stream.of("header1", "header2")
            .map(Util::randomizeCase)
            .collect(toList());
    SampleMethodHandler sampleMethodHandler = new SampleMethodHandler(requiredHeaders);
    sampleMethodHandler.registerPerAccept(mediaType1, mock(AcceptMapper.class));
    sampleMethodHandler.registerPerAccept(mediaType2, mock(AcceptMapper.class));
    sampleMethodHandler.registerPerAccept(mediaType3, mock(AcceptMapper.class));
    sampleMethodHandler.registerPerContentType(mediaType4, mock(ContentTypeMapper.class));
    supportedMethods.forEach(method -> handlerWithCORSSupport.registerMethodHandler(
            method,
            c -> sampleMethodHandler
    ));
    Map<String, String> headers = new ConcurrentHashMap<>();
    headers.put("Access-Control-Request-Method", methodBeingInvestigated);
    Collection<String> requestHeaders = requiredHeaders.stream()
            .map(Util::randomizeCase)
            .collect(toSet());
    requestHeaders.add("header3");
    headers.put("Access-Control-Request-Headers", String.join(", ", requestHeaders));
    headers.put("Content-Type", mediaType4.toString());
    randomiseKeyValues(headers);
    ApiGatewayProxyRequest request = new ApiGatewayProxyRequestBuilder()
            .withHttpMethod("OPTIONS")
            .withHeaders(headers)
            .withContext(context)
            .build();

    ApiGatewayProxyResponse response = handlerWithCORSSupport.handleRequest(request, context);

    assertThat(response.getStatusCode()).isEqualTo(BAD_REQUEST.getStatusCode());
    assertThat(response.getBody()).contains(String.format("Options method should include the %s header", ORIGIN_HEADER.toLowerCase()));
}
 
开发者ID:gabrielle-anderson,项目名称:aws-lambda-proxy-java,代码行数:42,代码来源:LambdaProxyHandlerTest.java


注:本文中的com.google.common.net.MediaType.create方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。