public void ChunkedTransferEncoding() { const string crlf = "\r\n"; const string response = "HTTP/1.1 200 OK" + crlf + "Content-Type: text/plain" + crlf + "Transfer-Encoding: chunked" + crlf + crlf + "7" + crlf + "Mozilla" + crlf + "9" + crlf + "Developer" + crlf + "7" + crlf + "Network" + crlf + "0" + crlf + crlf; var ascii = Encoding.ASCII; using var input = new MemoryStream(ascii.GetBytes(response)); using var hs = HttpMessageReader.ReadResponse(input); Assert.That(hs.Kind, Is.EqualTo(HttpMessageKind.Response)); Assert.That(hs.ProtocolVersion, Is.EqualTo(new Version(1, 1))); Assert.That(hs.StatusCode, Is.EqualTo(HttpStatusCode.OK)); Assert.That(hs.ReasonPhrase, Is.EqualTo("OK")); Assert.That(hs.Headers.Count, Is.EqualTo(2)); using var h = hs.Headers.GetEnumerator(); Assert.That(h.MoveNext(), Is.True); Assert.That(h.Current.Key, Is.EqualTo("Content-Type")); Assert.That(h.Current.Value, Is.EqualTo("text/plain")); Assert.That(h.MoveNext(), Is.True); Assert.That(h.Current.Key, Is.EqualTo("Transfer-Encoding")); Assert.That(h.Current.Value, Is.EqualTo("chunked")); Assert.That(h.MoveNext(), Is.False); using var output = new MemoryStream(); hs.ContentStream.CopyTo(output); var content = ascii.GetString(output.ToArray()); Assert.That(content, Is.EqualTo("MozillaDeveloperNetwork")); }
public void HeaderValuesAreOptional() { const string crlf = "\r\n"; const string response = "HTTP/1.1 200 OK" + crlf + "Content-Type: text/plain" + crlf + "Content-Length:" + crlf + "Content-Length: 0" + crlf + crlf; var ascii = Encoding.ASCII; using var input = new MemoryStream(ascii.GetBytes(response)); using var hs = HttpMessageReader.ReadResponse(input); Assert.That(hs.Kind, Is.EqualTo(HttpMessageKind.Response)); Assert.That(hs.ProtocolVersion, Is.EqualTo(new Version(1, 1))); Assert.That(hs.StatusCode, Is.EqualTo(HttpStatusCode.OK)); Assert.That(hs.ReasonPhrase, Is.EqualTo("OK")); Assert.That(hs.Headers.Count, Is.EqualTo(3)); using var h = hs.Headers.GetEnumerator(); Assert.That(h.MoveNext(), Is.True); Assert.That(h.Current.Key, Is.EqualTo("Content-Type")); Assert.That(h.Current.Value, Is.EqualTo("text/plain")); Assert.That(h.MoveNext(), Is.True); Assert.That(h.Current.Key, Is.EqualTo("Content-Length")); Assert.That(h.Current.Value, Is.EqualTo(string.Empty)); Assert.That(h.MoveNext(), Is.True); Assert.That(h.Current.Key, Is.EqualTo("Content-Length")); Assert.That(h.Current.Value, Is.EqualTo("0")); Assert.That(h.MoveNext(), Is.False); Assert.That(hs.ContentLength, Is.EqualTo((HttpFieldStatus.Defined, 0))); }