public void ResponseSentCorrect() { var headers = new Dictionary <string, string[]> { { "Connection", new[] { "close" } }, { "Content-Type", new [] { "text/plain" } } }; const string dataString = "test"; byte[] data = Encoding.UTF8.GetBytes(dataString); using (var stream = new MemoryStream()) { Http11Helper.SendResponse(stream, data, StatusCode.Code200Ok, headers); stream.Seek(0, SeekOrigin.Begin); string[] splittedResponse = Http11Helper.ReadHeaders(stream); byte[] response = new byte[data.Length];//test - 4 bytes int read = stream.Read(response, 0, response.Length); //Need to loop here... 4 bytes only - let's think that C# is in the good mood today. :-) Assert.Equal(read, response.Length); // let count number of items: // response string // Connection header // Content-Type header // Content-Length header // delimiter between headers and body - empty string // lines in response body Assert.Contains("HTTP/1.1", splittedResponse[0]); Assert.Contains(StatusCode.Code200Ok.ToString(CultureInfo.InvariantCulture), splittedResponse[0]); Assert.Contains(StatusCode.Reason200Ok, splittedResponse[0]); Assert.Contains("Connection: close", splittedResponse); Assert.Contains("Content-Type: " + "text/plain", splittedResponse); Assert.Contains("Content-Length: " + data.Length, splittedResponse); Assert.Equal(read, data.Length); } }
private void EndResponse(bool closeConnection = true) { byte[] bytes; // response body if (_response.Body is MemoryStream) { bytes = (_response.Body as MemoryStream).ToArray(); } else { bytes = new byte[0]; } Http11Helper.SendResponse(_client, bytes, _response.StatusCode, _response.Headers, closeConnection); if (closeConnection) { Http2Logger.LogDebug("Closing connection"); _client.Close(); } }
/// <summary> /// Completes response by sending result back to the client. /// </summary> /// <param name="ex">The error occured.</param> private void EndResponse(Exception ex) { int statusCode = (ex is NotSupportedException) ? StatusCode.Code501NotImplemented : StatusCode.Code500InternalServerError; Http11Helper.SendResponse(_client, new byte[0], statusCode); }