public void BodyContentFormData() { var content = "--boundary\n" + "Content-Disposition: form-data; name=\"field1\"\n\n" + "value1\n" + "--boundary\n" + "Content-Disposition: form-data; name=\"field2\"; filename=\"example.txt\"\n\n" + "value2\n" + "--boundary--"; var binaryMediaTypeDescriptor = "multipart/form-data;boundary=\"boundary\""; var binaryMediaType = ContentMediaType.ParseFromDescriptor(binaryMediaTypeDescriptor); var binaryRequest = Request.Has(Method.Post) .And(Version.Http1_1) .And("/user/my-post".ToMatchableUri()) .And(RequestHeader.FromString("Host:www.vlingo.io")) .And(RequestHeader.WithContentType(binaryMediaTypeDescriptor)) .And(Http.Body.From(content)); var resolver = ParameterResolver.Body <RequestData>(); var result = resolver.Apply(binaryRequest, _mappedParameters); var expected = new RequestData( Http.Body.From(content), ContentMediaType.ParseFromDescriptor(binaryMediaTypeDescriptor), ContentEncoding.None()); Assert.Equal(expected.MediaType, result.MediaType); Assert.Equal(expected.ContentEncoding, result.ContentEncoding); Assert.Equal(expected.Body.Content, result.Body.Content); Assert.Equal(ParameterResolver.Type.Body, resolver.Type); }
public void Parameters_do_not_affect_mapping() { var mediaTypeMapper = new MediaTypeMapper.Builder() .AddMapperFor(ContentMediaType.Json, DefaultJsonMapper.Instance) .Build(); var contentMediaType = ContentMediaType.ParseFromDescriptor("application/json; charset=UTF-8"); mediaTypeMapper.From(new object(), contentMediaType); }
public static ParameterResolver <T> Body <T>(MediaTypeMapper mediaTypeMapper) => ParameterResolver <T> .Create( Type.Body, (request, mappedParameters) => { var assumedBodyContentType = ContentMediaType.Json.ToString(); var bodyMediaType = request.HeaderValueOr(RequestHeader.ContentType, assumedBodyContentType); return(mediaTypeMapper.From <T>(request.Body?.ToString(), ContentMediaType.ParseFromDescriptor(bodyMediaType))); });
public void BuiltInTypesHaveCorrectFormat() { var jsonType = new ContentMediaType("application", "json"); Assert.Equal(jsonType, ContentMediaType.Json); var xmlType = new ContentMediaType("application", "xml"); Assert.Equal(xmlType, ContentMediaType.Xml); }
public string From <T>(T data, ContentMediaType contentMediaType) { var baseType = contentMediaType.ToBaseType(); if (_mappersByContentType.ContainsKey(baseType)) { return(_mappersByContentType[baseType].From(data) !); } throw new MediaTypeNotSupportedException(contentMediaType.ToString()); }
public Builder AddMapperFor(ContentMediaType contentMediaType, IMapper mapper) { if (_mappersByContentType.ContainsKey(contentMediaType)) { throw new InvalidOperationException("Content mimeType already added"); } _mappersByContentType[contentMediaType] = mapper; return(this); }
public static ParameterResolver <T> Body <T>(MediaTypeMapper mediaTypeMapper) { return(typeof(T).IsAssignableFrom(typeof(RequestData)) ? (ParameterResolver <T>)(object) ParameterResolver <RequestData> .Create(Type.Body, (request, mappedParameters) => { // This is a fall-back when content-type not provided for backwards compat for curl/cmd line users var bodyMediaType = BodyMediaTypeOrFallback(request); var contentEncodingHeader = request.Headers.HeaderOfOrDefault(ResponseHeader.ContentEncoding, RequestHeader.WithContentEncoding()); var contentEncoding = ContentEncoding.ParseFromHeader(contentEncodingHeader?.Value); return new RequestData(request.Body !, ContentMediaType.ParseFromDescriptor(bodyMediaType), contentEncoding); }) :
/// <summary> /// Serves as a hash function for a particular type. /// </summary> /// <returns> /// A hash code for the current <see cref="T:System.Object"/>. /// </returns> public override int GetHashCode() { unchecked { var hashCode = Type.GetHashCode(); hashCode = (hashCode * 397) ^ (Id?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (Schema?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (Comment?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (Title?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (Description?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (Default?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ MultipleOf.GetHashCode(); hashCode = (hashCode * 397) ^ Maximum.GetHashCode(); hashCode = (hashCode * 397) ^ ExclusiveMaximum.GetHashCode(); hashCode = (hashCode * 397) ^ Minimum.GetHashCode(); hashCode = (hashCode * 397) ^ ExclusiveMinimum.GetHashCode(); hashCode = (hashCode * 397) ^ (MaxLength?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (MinLength?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (Pattern?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (AdditionalItems?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (Items?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (MaxItems?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (MinItems?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (UniqueItems?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (Contains?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (AdditionalProperties?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (Definitions?.GetCollectionHashCode().GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (Properties?.GetCollectionHashCode() ?? 0); hashCode = (hashCode * 397) ^ (PatternProperties?.GetCollectionHashCode() ?? 0); hashCode = (hashCode * 397) ^ (Dependencies?.GetCollectionHashCode() ?? 0); hashCode = (hashCode * 397) ^ (Const?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (Enum?.GetCollectionHashCode() ?? 0); hashCode = (hashCode * 397) ^ (Format?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (ContentMediaType?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (ContentEncoding?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (If?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (Then?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (Else?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (AllOf?.GetCollectionHashCode() ?? 0); hashCode = (hashCode * 397) ^ (AnyOf?.GetCollectionHashCode() ?? 0); hashCode = (hashCode * 397) ^ (OneOf?.GetCollectionHashCode() ?? 0); hashCode = (hashCode * 397) ^ (Not?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (Required?.GetCollectionHashCode() ?? 0); hashCode = (hashCode * 397) ^ (Examples?.GetCollectionHashCode() ?? 0); return(hashCode); } }
public void BodyContentRequest() { var content = new byte[] { 0xD, 0xE, 0xA, 0xD, 0xB, 0xE, 0xE, 0xF }; var binaryMediaTypeDescriptor = "application/octet-stream"; var binaryMediaType = ContentMediaType.ParseFromDescriptor(binaryMediaTypeDescriptor); var binaryRequest = Request.Has(Method.Post) .And(Version.Http1_1) .And("/user/my-post".ToMatchableUri()) .And(RequestHeader.FromString("Host:www.vlingo.io")) .And(RequestHeader.WithContentType(binaryMediaTypeDescriptor)) .And(RequestHeader.WithContentEncoding(ContentEncodingMethod.Gzip.ToString())) .And(Http.Body.From(content, Http.Body.Encoding.None)); var resolver = ParameterResolver.Body <RequestData>(); var result = resolver.Apply(binaryRequest, _mappedParameters); var expected = new RequestData(Http.Body.From(content, Http.Body.Encoding.None), binaryMediaType, new ContentEncoding(ContentEncodingMethod.Gzip)); Assert.Equal(expected.ContentEncoding, result.ContentEncoding); Assert.Equal(expected.MediaType, result.MediaType); Assert.Equal(expected.Body.BinaryContent, result.Body.BinaryContent); Assert.Equal(ParameterResolver.Type.Body, resolver.Type); }
public Content(string data, ContentMediaType contentMediaType) { Data = data; ContentMediaType = contentMediaType; }
public RequestData(Body body, ContentMediaType mediaType, ContentEncoding contentEncoding) { Body = body; MediaType = mediaType; ContentEncoding = contentEncoding; }
internal bool IsSameOrSuperTypeOf(ContentMediaType contentMediaType) => (IsGenericType() || string.Equals(MimeType, contentMediaType.MimeType)) && (IsGenericSubType() || string.Equals(MimeSubType, contentMediaType.MimeSubType));