Пример #1
0
        public async Task ConnectTestAsync()
        {
            using HttpRequestMessage request = new(new HttpMethod("CONNECT"), "https://postman-echo.com");
            string plaintext = await HttpRequestMessageExtensions.ToHttpStringAsync(request, CancellationToken.None);

            Assert.Equal("CONNECT / HTTP/1.1\r\n\r\n", plaintext);
        }
Пример #2
0
        public async Task GetTestAsync()
        {
            using HttpRequestMessage request = new(HttpMethod.Get, "https://postman-echo.com");
            string plaintext = await HttpRequestMessageExtensions.ToHttpStringAsync(request, CancellationToken.None);

            Assert.Equal("GET / HTTP/1.1\r\nHost:postman-echo.com\r\n\r\n", plaintext);
        }
Пример #3
0
        public async Task PostTestAsync()
        {
            // JSON string with HEX content.
            using var content = new StringContent(content: @"{""key"": ""value""}", Encoding.UTF8, "application/json");
            using HttpRequestMessage request = new(HttpMethod.Post, "https://postman-echo.com");
            request.Content = content;

            string actualPlaintext = await HttpRequestMessageExtensions.ToHttpStringAsync(request, CancellationToken.None);

            string expected = "POST / HTTP/1.1\r\nHost:postman-echo.com\r\nContent-Type:application/json; charset=utf-8\r\nContent-Length:16\r\n\r\n{\"key\": \"value\"}";

            Assert.Equal(expected, actualPlaintext);
        }
Пример #4
0
        protected override Task <HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            string uriString             = this._rootUrlResolver(request) + "/" + this._redirectPath;
            HttpResponseMessage response = HttpRequestMessageExtensions.CreateResponse(request, HttpStatusCode.MovedPermanently);

            response.Headers.Location = new Uri(uriString);
            TaskCompletionSource <HttpResponseMessage> completionSource = new TaskCompletionSource <HttpResponseMessage>();

            completionSource.SetResult(response);
            return(completionSource.Task);
        }
Пример #5
0
        private static Uri GetNestedNextPageLink(ODataSerializerContext writeContext, int pageSize)
        {
            Contract.Assert(writeContext.ExpandedEntity != null);

            IEdmNavigationSource sourceNavigationSource       = writeContext.ExpandedEntity.NavigationSource;
            NavigationSourceLinkBuilderAnnotation linkBuilder = writeContext.Model.GetNavigationSourceLinkBuilder(sourceNavigationSource);
            Uri navigationLink =
                linkBuilder.BuildNavigationLink(writeContext.ExpandedEntity, writeContext.NavigationProperty);

            if (navigationLink != null)
            {
                return(HttpRequestMessageExtensions.GetNextPageLink(navigationLink, pageSize));
            }

            return(null);
        }
Пример #6
0
        protected override Task <HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            IAssetProvider swaggerUiProvider = this._config.GetSwaggerUiProvider();
            string         rootUrl           = this._config.GetRootUrl(request);
            string         assetPath         = HttpRequestMessageExtensions.GetRouteData(request).get_Values()["assetPath"].ToString();

            try
            {
                HttpContent httpContent = this.ContentFor(swaggerUiProvider.GetAsset(rootUrl, assetPath));
                return(this.TaskFor(new HttpResponseMessage()
                {
                    Content = httpContent,
                    RequestMessage = request
                }));
            }
            catch (AssetNotFound ex)
            {
                return(this.TaskFor(HttpRequestMessageExtensions.CreateErrorResponse(request, HttpStatusCode.NotFound, (Exception)ex)));
            }
        }
Пример #7
0
        protected override Task <HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            ISwaggerProvider swaggerProvider = this._config.GetSwaggerProvider(request);
            string           rootUrl         = this._config.GetRootUrl(request);
            string           apiVersion      = HttpRequestMessageExtensions.GetRouteData(request).get_Values()["apiVersion"].ToString();

            try
            {
                SwaggerDocument swagger     = swaggerProvider.GetSwagger(rootUrl, apiVersion);
                HttpContent     httpContent = this.ContentFor(request, swagger);
                return(this.TaskFor(new HttpResponseMessage()
                {
                    Content = httpContent
                }));
            }
            catch (UnknownApiVersion ex)
            {
                return(this.TaskFor(HttpRequestMessageExtensions.CreateErrorResponse(request, HttpStatusCode.NotFound, (Exception)ex)));
            }
        }
Пример #8
0
        internal ISwaggerProvider GetSwaggerProvider(HttpRequestMessage swaggerRequest)
        {
            HttpConfiguration configuration = HttpRequestMessageExtensions.GetConfiguration(swaggerRequest);
            Dictionary <string, SecurityScheme> dictionary = this._securitySchemeBuilders.Any <KeyValuePair <string, SecuritySchemeBuilder> >() ? this._securitySchemeBuilders.ToDictionary <KeyValuePair <string, SecuritySchemeBuilder>, string, SecurityScheme>((Func <KeyValuePair <string, SecuritySchemeBuilder>, string>)(kvp => kvp.Key), (Func <KeyValuePair <string, SecuritySchemeBuilder>, SecurityScheme>)(kvp => kvp.Value.Build())) : (Dictionary <string, SecurityScheme>)null;
            List <IModelFilter>     list1 = this._modelFilters.Select <Func <IModelFilter>, IModelFilter>((Func <Func <IModelFilter>, IModelFilter>)(factory => factory())).ToList <IModelFilter>();
            List <IOperationFilter> list2 = this._operationFilters.Select <Func <IOperationFilter>, IOperationFilter>((Func <Func <IOperationFilter>, IOperationFilter>)(factory => factory())).ToList <IOperationFilter>();

            foreach (Func <XPathDocument> xmlDocFactory in (IEnumerable <Func <XPathDocument> >) this._xmlDocFactories)
            {
                XPathDocument xmlDoc = xmlDocFactory();
                list1.Insert(0, (IModelFilter) new ApplyXmlTypeComments(xmlDoc));
                list2.Insert(0, (IOperationFilter) new ApplyXmlActionComments(xmlDoc));
            }
            SwaggerGeneratorOptions options          = new SwaggerGeneratorOptions(this._versionSupportResolver, this._schemes, (IDictionary <string, SecurityScheme>)dictionary, this._ignoreObsoleteActions, this._groupingKeySelector, this._groupingKeyComparer, this._customSchemaMappings, (IEnumerable <ISchemaFilter>) this._schemaFilters.Select <Func <ISchemaFilter>, ISchemaFilter>((Func <Func <ISchemaFilter>, ISchemaFilter>)(factory => factory())).ToList <ISchemaFilter>(), (IEnumerable <IModelFilter>)list1, this._ignoreObsoleteProperties, this._schemaIdSelector, this._describeAllEnumsAsStrings, this._describeStringEnumsInCamelCase, this._applyFiltersToAllSchemas, (IEnumerable <IOperationFilter>)list2, (IEnumerable <IDocumentFilter>) this._documentFilters.Select <Func <IDocumentFilter>, IDocumentFilter>((Func <Func <IDocumentFilter>, IDocumentFilter>)(factory => factory())).ToList <IDocumentFilter>(), this._conflictingActionsResolver);
            SwaggerGenerator        swaggerGenerator = new SwaggerGenerator(ServicesExtensions.GetApiExplorer(configuration.get_Services()), configuration.SerializerSettingsOrDefault(), this._versionInfoBuilder.Build(), options);

            if (this._customProviderFactory == null)
            {
                return((ISwaggerProvider)swaggerGenerator);
            }
            return(this._customProviderFactory((ISwaggerProvider)swaggerGenerator));
        }
Пример #9
0
        private HttpContent ContentFor(
            HttpRequestMessage request,
            SwaggerDocument swaggerDoc)
        {
            ContentNegotiationResult negotiationResult = ServicesExtensions.GetContentNegotiator(HttpRequestMessageExtensions.GetConfiguration(request).get_Services()).Negotiate(typeof(SwaggerDocument), request, this.GetSupportedSwaggerFormatters());

            return((HttpContent) new ObjectContent(typeof(SwaggerDocument), (object)swaggerDoc, negotiationResult.get_Formatter(), negotiationResult.get_MediaType()));
        }
Пример #10
0
 public static string DefaultRootUrlResolver(HttpRequestMessage request)
 {
     return(new UriBuilder(SwaggerDocsConfig.GetHeaderValue(request, "X-Forwarded-Proto") ?? request.RequestUri.Scheme, SwaggerDocsConfig.GetHeaderValue(request, "X-Forwarded-Host") ?? request.RequestUri.Host, int.Parse(SwaggerDocsConfig.GetHeaderValue(request, "X-Forwarded-Port") ?? request.RequestUri.Port.ToString((IFormatProvider)CultureInfo.InvariantCulture)), (SwaggerDocsConfig.GetHeaderValue(request, "X-Forwarded-Prefix") ?? string.Empty) + HttpRequestMessageExtensions.GetConfiguration(request).get_VirtualPathRoot()).Uri.AbsoluteUri.TrimEnd('/'));
 }
        public void GetBodyAsync_HttpRequestMessageIsNull_ThrowsArgumentNullException()
        {
            var exception = Assert.ThrowsAsync <ArgumentNullException>(() => HttpRequestMessageExtensions.GetBodyAsync(null));

            Assert.Equal("httpRequest", exception.Result.ParamName);
        }