當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。