public dynamic[] ConvertJavaScriptResponse(CommonObject obj) { string status = null; var headerDictionary = new Hash(); dynamic body = null; foreach (var prop in obj.Members) { if (prop.Key == "headers") { var headers = prop.Value as CommonObject; if (headers == null) { continue; } foreach (var header in headers.Members) { headerDictionary[header.Key] = header.Value.ToString(); } continue; } if (prop.Key == "status") { status = prop.Value.ToString(); continue; } if (prop.Key == "body") { body = prop.Value; if (body is ArrayObject) { var arr = (ArrayObject)prop.Value; var responseBody = new List <string>(); for (var i = 0; i < arr.Length; i++) { responseBody.Add(arr.Get(i).Object.ToString()); } body = new IterableAdapter(responseBody); } } } return(new[] { status, headerDictionary, body }); }
public void Should_Not_Set_Content_Length_On_Variable_Length_Bodies() { Func <string> body = () => "Hello World!"; var iterableBody = new IterableAdapter(new Proc(body), lambda => lambda.Call()); var app = DetachedApplication.Create( env => new dynamic[] { 200, new Hash { { "Content-Type", "text/plain" } }, iterableBody }); var response = new ContentLength(app).Call(new Dictionary <string, dynamic>()); Assert.IsFalse(response[1].ContainsKey("Content-Length")); }
public MockResponse(dynamic[] responseArray) { Status = Convert.ToInt32(responseArray[0]); OriginalHeaders = responseArray[1]; Headers = (Hash)responseArray[1]; var bodyList = new List <string>(); var body = responseArray[2] as IIterable; if (body == null) { body = new IterableAdapter(responseArray[2]); } body.Each(part => { if (part is byte[]) { bodyList.Add(System.Text.Encoding.ASCII.GetString(part)); } else { bodyList.Add(part.ToString()); } }); Body = new IterableAdapter(bodyList); var errors = responseArray.Length > 3 ? responseArray[3] : new MemoryStream(); if (errors is MemoryStream) { var errorStream = (MemoryStream)errors; var pos = errorStream.Position; errorStream.Position = 0; var reader = new StreamReader(errorStream); var errorString = reader.ReadToEnd(); errorStream.Position = pos; Errors = errorString; } }