public void CanParse_MediaTypesWithParameters(string mediaType) { // Arrange & Act var result = new MediaType(mediaType, 0, mediaType.Length); // Assert Assert.Equal(new StringSegment("application"), result.Type); Assert.Equal(new StringSegment("json"), result.SubType); Assert.Equal(new StringSegment("pretty"), result.GetParameter("format")); Assert.Equal(new StringSegment("0.8"), result.GetParameter("q")); Assert.Equal(new StringSegment("utf-8"), result.GetParameter("charset")); }
public void Constructor_CanParseMediaTypesWithParameters(string mediaType) { // Arrange & Act var result = new MediaType(mediaType, 0, mediaType.Length); // Assert new StringSegment("application").Should().Be(result.Type); new StringSegment("json+bson").Should().Be(result.SubType); new StringSegment("json").Should().Be(result.SubTypeWithoutSuffix); new StringSegment("bson").Should().Be(result.SubTypeSuffix); new StringSegment("pretty").Should().Be(result.GetParameter("format")); new StringSegment("0.8").Should().Be(result.GetParameter("q")); new StringSegment("utf-8").Should().Be(result.GetParameter("charset")); }
/// <summary> /// Replaces the encoding of the given <paramref name="mediaType"/> with the provided /// <paramref name="encoding"/>. /// </summary> /// <param name="mediaType">The media type whose encoding will be replaced.</param> /// <param name="encoding">The encoding that will replace the encoding in the <paramref name="mediaType"/>. /// </param> /// <returns>A media type with the replaced encoding.</returns> public static string ReplaceEncoding(StringSegment mediaType, Encoding encoding) { var parsedMediaType = new MediaType(mediaType); var charset = parsedMediaType.GetParameter("charset"); if (charset.HasValue && charset.Equals(encoding.WebName, StringComparison.OrdinalIgnoreCase)) { return(mediaType.Value ?? string.Empty); } if (!charset.HasValue) { return(CreateMediaTypeWithEncoding(mediaType, encoding)); } var charsetOffset = charset.Offset - mediaType.Offset; var restOffset = charsetOffset + charset.Length; var restLength = mediaType.Length - restOffset; var finalLength = charsetOffset + encoding.WebName.Length + restLength; var builder = new StringBuilder(mediaType.Buffer, mediaType.Offset, charsetOffset, finalLength); builder.Append(encoding.WebName); builder.Append(mediaType.Buffer, restOffset, restLength); return(builder.ToString()); }
public void WriteMultipartWithIResource() { string fileToUpload = Path.Combine(Environment.CurrentDirectory, @"Http\Converters\FileToUpload.png"); IDictionary <string, object> parts = new Dictionary <string, object>(); parts.Add("name 1", "value 1"); parts.Add("name 2", "value 2+1"); HttpEntity entity = new HttpEntity("<root><child/></root>"); entity.Headers.ContentType = MediaType.TEXT_XML; parts.Add("xml", entity); parts.Add("logo", new FileResource(fileToUpload)); MockHttpOutputMessage message = new MockHttpOutputMessage(); converter.Write(parts, MediaType.MULTIPART_FORM_DATA, message); MediaType contentType = message.Headers.ContentType; Assert.IsNotNull(contentType, "Invalid content-type"); Assert.AreEqual("multipart", contentType.Type, "Invalid content-type"); Assert.AreEqual("form-data", contentType.Subtype, "Invalid content-type"); string boundary = contentType.GetParameter("boundary"); Assert.IsNotNull(boundary, "Invalid content-type"); string result = message.GetBodyAsString(Encoding.UTF8); Assert.IsTrue(result.Contains("--" + boundary + "\r\nContent-Disposition: form-data; name=\"name 1\"\r\nContent-Type: text/plain;charset=ISO-8859-1\r\n\r\nvalue 1\r\n"), "Invalid content-disposition"); Assert.IsTrue(result.Contains("--" + boundary + "\r\nContent-Disposition: form-data; name=\"name 2\"\r\nContent-Type: text/plain;charset=ISO-8859-1\r\n\r\nvalue 2+1\r\n"), "Invalid content-disposition"); Assert.IsTrue(result.Contains("--" + boundary + "\r\nContent-Disposition: form-data; name=\"xml\"\r\nContent-Type: text/xml\r\n\r\n<root><child/></root>\r\n"), "Invalid content-disposition"); Assert.IsTrue(result.Contains("--" + boundary + "\r\nContent-Disposition: form-data; name=\"logo\"; filename=\"FileToUpload.png\"\r\nContent-Type: image/png\r\n\r\n"), "Invalid content-disposition"); }
public void GetParameter_ReturnsNull_IfParameterIsNotInMediaType() { var mediaType = "application/json;charset=utf-8;format=indent;q=0.8"; var parsedMediaType = new MediaType(mediaType, 0, mediaType.Length); // Act var result = parsedMediaType.GetParameter("other"); // Assert Assert.False(result.HasValue); }
public void GetParameter_ReturnsParameter_IfParameterIsInMediaType(string mediaType) { // Arrange var expectedParameter = new StringSegment("utf-8"); var parsedMediaType = new MediaType(mediaType, 0, mediaType.Length); // Act var result = parsedMediaType.GetParameter("charset"); // Assert expectedParameter.Should().Be(result); }
public void GetParameter_ReturnsNull_IfParameterIsNotInMediaType() { var mediaType = "application/json;charset=utf-8;format=indent;q=0.8"; var parsedMediaType = new MediaType(mediaType, 0, mediaType.Length); // Act var result = parsedMediaType.GetParameter("other"); // Assert Assert.False(result.HasValue); }
public void GetParameter_IsCaseInsensitive() { // Arrange var mediaType = "application/json;charset=utf-8"; var expectedParameter = new StringSegment("utf-8"); var parsedMediaType = new MediaType(mediaType); // Act var result = parsedMediaType.GetParameter("CHARSET"); // Assert expectedParameter.Should().Be(result); }
public void GetParameter_IsCaseInsensitive() { // Arrange var mediaType = "application/json;charset=utf-8"; var expectedParameter = new StringSegment("utf-8"); var parsedMediaType = new MediaType(mediaType); // Act var result = parsedMediaType.GetParameter("CHARSET"); // Assert Assert.NotNull(result); Assert.Equal(expectedParameter, result); }
public void GetParameter_ReturnsParameter_IfParameterIsInMediaType(string mediaType) { // Arrange var expectedParameter = new StringSegment("utf-8"); var parsedMediaType = new MediaType(mediaType, 0, mediaType.Length); // Act var result = parsedMediaType.GetParameter("charset"); // Assert Assert.NotNull(result); Assert.Equal(expectedParameter, result); }