本文整理汇总了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()));
}
示例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()));
}
示例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()))));
}
示例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);
}
示例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()));
}