Abstract of an strongly typed object response.
상속: ResponseInfo
예제 #1
0
        private ResponseInfo CreateFakeResponseInfo(RequestInfo fakeRequest, IEnumerable <string> contentType, params object[] bodyArguments)
        {
            ResponseInfo         result;
            IEnumerable <string> currentContentType;

            if (bodyArguments.Length == 1)
            {
                var singleResponse = ObjectResponseInfo <object> .CreateInstance(Encoding.UTF8, fakeRequest, bodyArguments[0].GetType(), bodyArguments[0], _converterProvider);

                currentContentType = (singleResponse.Headers[Header.ContentType] == null ? new string[0] :
                                      new[] { singleResponse.Headers[Header.ContentType].Values.Select(item => item.Value).First() });
                result = singleResponse;
            }
            else
            {
                var multiResponse = new MultiObjectResponseInfo(Encoding.UTF8, fakeRequest, bodyArguments, _converterProvider);
                currentContentType = multiResponse.ObjectResponses.Select(item => item.Headers[Header.ContentType].Values.Select(value => value.Value).First())
                                     .Where(item => !String.IsNullOrEmpty(item));
                result = multiResponse;
            }

            if ((contentType.Any()) && (currentContentType.Join(contentType, outer => outer, inner => inner, (outer, inner) => inner).Count() != currentContentType.Count()))
            {
                throw new WebException("Unsupported media type", WebExceptionStatus.ProtocolError);
            }

            return(result);
        }
예제 #2
0
        private ResponseInfo MakeListResponse(IRequestMapping requestMapping, IEnumerable resultingValues, object[] arguments, int totalItems = 0)
        {
            var          status = HttpStatusCode.OK;
            ResponseInfo result = (ResponseInfo)requestMapping.Target.Response;

            if ((arguments[1] != null) && (arguments[2] != null))
            {
                int skip = (int)arguments[1];
                int take = (int)arguments[2];
                take = (take == 0 ? totalItems : Math.Min(take, resultingValues.Cast <object>().Count()));
                var contentRangeHeaderValue = String.Format("members {0}-{1}/{2}", skip, Math.Max(0, take - 1), totalItems);
                result.Headers.Add(new Header("Content-Range", contentRangeHeaderValue));
                status = HttpStatusCode.PartialContent;
            }

            result = ObjectResponseInfo <object> .CreateInstance(
                result.Encoding,
                result.Request,
                requestMapping.Operation.UnderlyingMethod.ReturnType,
                resultingValues,
                _converterProvider,
                result.Headers);

            result.Status = status;
            return(result);
        }
예제 #3
0
        private ResponseInfo MakeResponse(IRequestMapping requestMapping, IList <object> resultingValues)
        {
            ResponseInfo result       = (ResponseInfo)requestMapping.Target.Response;
            var          bodyValues   = new List <KeyValuePair <ResultInfo, object> >();
            var          headerValues = new List <KeyValuePair <ResultInfo, object> >();

            requestMapping.Operation.Results.ForEach((resultValue, index) =>
                                                     (resultValue.Target is ToHeaderAttribute ? headerValues : bodyValues).Add(new KeyValuePair <ResultInfo, object>(resultValue, resultingValues[index])));

            switch (bodyValues.Count)
            {
            case 0:
                break;

            case 1:
                var bodyValue = bodyValues[0];
                result = ObjectResponseInfo <object> .CreateInstance(result.Encoding, result.Request, bodyValue.Value.GetType(), bodyValue.Value, _converterProvider, result.Headers);

                break;

            default:
                result = new MultiObjectResponseInfo(result.Encoding, result.Request, bodyValues.Select(item => item.Value), _converterProvider, result.Headers);
                break;
            }

            headerValues.ForEach(value => result.Headers.Add(new Header(
                                                                 ((ToHeaderAttribute)value.Key.Target).Name,
                                                                 String.Format(((ToHeaderAttribute)value.Key.Target).Format, _converterProvider.ConvertFrom(value.Value)))));
            return(result);
        }
예제 #4
0
        private void Initialize(Encoding encoding, RequestInfo request, IEnumerable <object> values, IConverterProvider converterProvider)
        {
            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            var boundary = Guid.NewGuid().ToString();

            Headers.ContentType = String.Format("multipart/mixed; boundary=\"{0}\"", boundary);
            _body = new MemoryStream();
            Body  = new UnclosableStream(_body);
            var valueResponses = new List <ResponseInfo>();

            using (var writer = new StreamWriter(Body))
            {
                foreach (var value in values.Where(value => value != null))
                {
                    writer.Write("--{0}\r\n", boundary);
                    var objectResponse = ObjectResponseInfo <object> .CreateInstance(encoding, request, value.GetType(), value, converterProvider);

                    valueResponses.Add(objectResponse);
                    foreach (var header in objectResponse.Headers)
                    {
                        switch (header.Name)
                        {
                        case "Content-Lenght":
                            break;

                        default:
                            writer.Write("{0}\r\n", header);
                            break;
                        }
                    }

                    writer.Write("Content-Length:{0}\r\n\r\n", objectResponse.Body.Length);
                    writer.Flush();
                    objectResponse.Body.CopyTo(Body);
                    writer.Flush();
                    writer.Write("\r\n");
                    writer.Flush();
                }

                writer.Write("--{0}--", boundary);
                writer.Flush();
            }

            _body.Seek(0, SeekOrigin.Begin);
            ObjectResponses = valueResponses;
        }
예제 #5
0
        private ResponseInfo MakeGetResponse(IRequestMapping requestMapping, object resultingValue)
        {
            ResponseInfo result = (ResponseInfo)requestMapping.Target.Response;

            if (resultingValue == null)
            {
                result.Status = HttpStatusCode.NotFound;
                return(result);
            }

            result = ObjectResponseInfo <object> .CreateInstance(result.Encoding, result.Request, resultingValue.GetType(), resultingValue, _converterProvider, result.Headers);

            return(result);
        }
예제 #6
0
 public void Setup()
 {
     (_converter = new Mock<IConverter>(MockBehavior.Strict)).Setup(instance => instance.CanConvertFrom<string>(It.IsAny<IResponseInfo>()))
         .Returns<IResponseInfo>(response => CompatibilityLevel.ExactMatch);
     (_converterProvider = new Mock<IConverterProvider>(MockBehavior.Strict)).Setup(instance => instance.FindBestOutputConverter<string>(It.IsAny<IResponseInfo>()))
         .Returns<IResponseInfo>(response => _converter.Object);
     _converter.Setup(instance => instance.ConvertFrom(Body, It.IsAny<ObjectResponseInfo<string>>()))
         .Callback<string, IResponseInfo>((body, response) =>
         {
             using (var writer = new StreamWriter(response.Body))
             {
                 writer.Write(body);
                 writer.Flush();
             }
         });
     _request = new RequestInfo(Verb.GET, (HttpUrl)UrlParser.Parse("http://temp.org/"), new MemoryStream(), new BasicClaimBasedIdentity());
     _response = new ObjectResponseInfo<string>(_request, Body, _converterProvider.Object);
 }