Esempio n. 1
0
        public async Task AddContextXForwardedToRequests()
        {
            var mockRequestExecutor = Substitute.For <IRequestExecutor>();

            mockRequestExecutor
            .GetAsync(Arg.Any <string>(), Arg.Any <IEnumerable <KeyValuePair <string, string> > >(), Arg.Any <CancellationToken>())
            .Returns(new HttpResponse <string>()
            {
                StatusCode = 200
            });

            var dataStore = new DefaultDataStore(mockRequestExecutor, new DefaultSerializer(), new ResourceFactory(null, null), NullLogger.Instance);
            var request   = new HttpRequest {
                Uri = "https://foo.dev"
            };
            var requestContext = new RequestContext
            {
                XForwardedFor   = "myapp.com",
                XForwardedPort  = "1234",
                XForwardedProto = "https",
            };

            await dataStore.GetAsync <TestResource>(request, requestContext, CancellationToken.None);

            // Assert that the request sent to the RequestExecutor included the User-Agent header
            await mockRequestExecutor.Received().GetAsync(
                "https://foo.dev",
                Arg.Is <IEnumerable <KeyValuePair <string, string> > >(headers =>
                                                                       headers.Any(kvp => kvp.Key == "X-Forwarded-For" && kvp.Value == "myapp.com") &&
                                                                       headers.Any(kvp => kvp.Key == "X-Forwarded-Port" && kvp.Value == "1234") &&
                                                                       headers.Any(kvp => kvp.Key == "X-Forwarded-Proto" && kvp.Value == "https")),
                CancellationToken.None);
        }
Esempio n. 2
0
        public async Task AddContextUserAgentToRequests()
        {
            var mockRequestExecutor = Substitute.For <IRequestExecutor>();

            mockRequestExecutor
            .GetAsync(Arg.Any <string>(), Arg.Any <IEnumerable <KeyValuePair <string, string> > >(), Arg.Any <CancellationToken>())
            .Returns(new HttpResponse <string>()
            {
                StatusCode = 200
            });

            var dataStore = new DefaultDataStore(mockRequestExecutor, new DefaultSerializer(), new ResourceFactory(null, null), NullLogger.Instance);
            var request   = new HttpRequest {
                Uri = "https://foo.dev"
            };
            var requestContext = new RequestContext {
                UserAgent = "sdk-vanillajs/1.1"
            };

            await dataStore.GetAsync <TestResource>(request, requestContext, CancellationToken.None);

            // Assert that the request sent to the RequestExecutor included the User-Agent header
            await mockRequestExecutor.Received().GetAsync(
                "https://foo.dev",
                Arg.Is <IEnumerable <KeyValuePair <string, string> > >(
                    headers => headers.Any(kvp => kvp.Key == "User-Agent" && kvp.Value.StartsWith("sdk-vanillajs/1.1 okta-sdk-dotnet/"))),
                CancellationToken.None);
        }
Esempio n. 3
0
        public async Task DoNotOvewriteUserAgentWhenProvided()
        {
            var mockRequestExecutor = Substitute.For <IRequestExecutor>();

            mockRequestExecutor
            .GetAsync(Arg.Any <string>(), Arg.Any <IEnumerable <KeyValuePair <string, string> > >(), Arg.Any <CancellationToken>())
            .Returns(new HttpResponse <string>()
            {
                StatusCode = 200
            });

            var dataStore = new DefaultDataStore(mockRequestExecutor, new DefaultSerializer(), new ResourceFactory(null, null, null), NullLogger.Instance, new UserAgentBuilder("okta-sdk-dotnet", UserAgentHelper.SdkVersion));
            var request   = new HttpRequest {
                Uri = "https://foo.dev"
            };

            request.Headers["User-Agent"] = "foo bar baz";

            await dataStore.GetAsync <TestResource>(request, new RequestContext(), CancellationToken.None);

            // Assert that the request sent to the RequestExecutor included the User-Agent header
            await mockRequestExecutor.Received().GetAsync(
                "https://foo.dev",
                Arg.Is <IEnumerable <KeyValuePair <string, string> > >(
                    headers => headers.Any(kvp => kvp.Key == "User-Agent" && kvp.Value == "foo bar baz")),
                CancellationToken.None);
        }
Esempio n. 4
0
        public async Task DeserializeObjectUsingCustomSettings()
        {
            var mockResponse = @"{  
               ""id"":""foo"",
               ""status"":""ACTIVE"",
               ""created"":""2019-05-09T15:37:44.000Z"",
               ""activated"":""2019-05-09T15:37:44.000Z"",
               ""statusChanged"":""2019-05-09T18:29:29.000Z"",
               ""lastLogin"":""2019-08-09T07:49:51.000Z"",
               ""lastUpdated"":""2019-08-09T17:20:28.000Z"",
               ""passwordChanged"":""2019-05-09T18:29:29.000Z"",
               ""profile"":{  
                  ""firstName"":""John"",
                  ""lastName"":""Coder"",
                  ""mobilePhone"":null,
                  ""userType"":""test"",
                  ""login"":""*****@*****.**"",
                  ""startDate"":""2016-06-07T00:00:00.000"",
                  ""email"":""*****@*****.**"",
                  ""employeeNumber"":""000012345"",
                  ""type"":""null"",
               },
            }";

            var mockRequestExecutor = Substitute.For <IRequestExecutor>();

            mockRequestExecutor
            .GetAsync(Arg.Any <string>(), Arg.Any <IEnumerable <KeyValuePair <string, string> > >(), Arg.Any <CancellationToken>())
            .Returns(new HttpResponse <string>()
            {
                StatusCode = 200, Payload = mockResponse
            });

            // DateParseHandling.None: Date formatted strings are not parsed to a date type and are read as strings.
            var dataStore = new DefaultDataStore(mockRequestExecutor, new DefaultSerializer(new JsonSerializerSettings()
            {
                DateParseHandling = DateParseHandling.None
            }), new ResourceFactory(null, null), NullLogger.Instance);
            var request = new HttpRequest {
                Uri = "https://foo.dev", Payload = new { }
            };
            var response = await dataStore.GetAsync <User>(request, new RequestContext(), CancellationToken.None);

            response.Payload.Profile["startDate"].GetType().Should().Be(typeof(string));
            response.Payload.Profile["startDate"].Should().Be("2016-06-07T00:00:00.000");

            // DateParseHandling.DateTimeOffset: Date formatted strings, e.g. "\/Date(1198908717056)\/" and "2012-03-21T05:40Z", are parsed to DateTimeOffset.
            dataStore = new DefaultDataStore(mockRequestExecutor, new DefaultSerializer(new JsonSerializerSettings()
            {
                DateParseHandling = DateParseHandling.DateTimeOffset
            }), new ResourceFactory(null, null), NullLogger.Instance);
            response = await dataStore.GetAsync <User>(request, new RequestContext(), CancellationToken.None);

            response.Payload.Profile["startDate"].GetType().Should().Be(typeof(DateTimeOffset));
            var dateTimeOffset = DateTimeOffset.Parse("2016-06-07T00:00:00.000");

            response.Payload.Profile["startDate"].Should().Be(dateTimeOffset);
        }
        public async Task ThrowForNullExecutorResponseDuringGet()
        {
            // If the RequestExecutor returns a null HttpResponse, throw an informative exception.

            var mockRequestExecutor = Substitute.For <IRequestExecutor>();
            var dataStore           = new DefaultDataStore(mockRequestExecutor, new DefaultSerializer(), new ResourceFactory(null, null), NullLogger.Instance);
            var request             = new HttpRequest {
                Uri = "https://foo.dev"
            };

            await Assert.ThrowsAsync <InvalidOperationException>(
                () => dataStore.GetAsync <TestResource>(request, new RequestContext(), CancellationToken.None));
        }
Esempio n. 6
0
        public async Task HandleEmptyPayloadDuringGet()
        {
            // If the API returns a null or empty payload, it shouldn't cause an error.

            var requestExecutor = new MockedStringRequestExecutor(string.Empty, statusCode: 200);
            var dataStore       = new DefaultDataStore(requestExecutor, new DefaultSerializer(), new ResourceFactory(null, null), NullLogger.Instance);
            var request         = new HttpRequest {
                Uri = "https://foo.dev"
            };

            var response = await dataStore.GetAsync <TestResource>(request, new RequestContext(), CancellationToken.None);

            response.StatusCode.Should().Be(200);
            response.Payload.Should().NotBeNull();
            response.Payload.Foo.Should().BeNullOrEmpty();
        }
Esempio n. 7
0
        public async Task DeserializeDateCustomPropertyAsString()
        {
            var mockResponse = @"{  
               ""id"":""foo"",
               ""status"":""ACTIVE"",
               ""created"":""2019-05-09T15:37:44.000Z"",
               ""activated"":""2019-05-09T15:37:44.000Z"",
               ""statusChanged"":""2019-05-09T18:29:29.000Z"",
               ""lastLogin"":""2019-08-09T07:49:51.000Z"",
               ""lastUpdated"":""2019-08-09T17:20:28.000Z"",
               ""passwordChanged"":""2019-05-09T18:29:29.000Z"",
               ""profile"":{  
                  ""firstName"":""John"",
                  ""lastName"":""Coder"",
                  ""mobilePhone"":null,
                  ""userType"":""test"",
                  ""login"":""*****@*****.**"",
                  ""startDate"":""2016-06-07T00:00:00.000"",
                  ""email"":""*****@*****.**"",
                  ""employeeNumber"":""000012345"",
                  ""type"":""null"",
               },
            }";

            var mockRequestExecutor = Substitute.For <IRequestExecutor>();

            mockRequestExecutor
            .GetAsync(Arg.Any <string>(), Arg.Any <IEnumerable <KeyValuePair <string, string> > >(), Arg.Any <CancellationToken>())
            .Returns(new HttpResponse <string>()
            {
                StatusCode = 200, Payload = mockResponse
            });

            var dataStore = new DefaultDataStore(mockRequestExecutor, new DefaultSerializer(), new ResourceFactory(null, null), NullLogger.Instance);
            var request   = new HttpRequest {
                Uri = "https://foo.dev", Payload = new { }
            };
            var response = await dataStore.GetAsync <User>(request, new RequestContext(), CancellationToken.None);

            response.Payload.Profile["startDate"].GetType().Should().Be(typeof(string));
            response.Payload.Profile["startDate"].Should().Be("2016-06-07T00:00:00.000");
        }
Esempio n. 8
0
        public async Task DelegateGetToRequestExecutor()
        {
            var mockRequestExecutor = Substitute.For <IRequestExecutor>();

            mockRequestExecutor
            .GetAsync(Arg.Any <string>(), Arg.Any <IEnumerable <KeyValuePair <string, string> > >(), Arg.Any <CancellationToken>())
            .Returns(new HttpResponse <string>()
            {
                StatusCode = 200
            });

            var dataStore = new DefaultDataStore(mockRequestExecutor, new DefaultSerializer(), new ResourceFactory(null, null), NullLogger.Instance);
            var request   = new HttpRequest {
                Uri = "https://foo.dev"
            };

            await dataStore.GetAsync <TestResource>(request, new RequestContext(), CancellationToken.None);

            await mockRequestExecutor.Received().GetAsync(
                "https://foo.dev",
                Arg.Any <IEnumerable <KeyValuePair <string, string> > >(),
                CancellationToken.None);
        }
        public async Task NotSendCloseConnectionHeaderWhenUsingTheDefaultClient()
        {
            var testOktaClient = TestClient.Create();
            var configuration  = testOktaClient.Configuration;
            var httpClient     = DefaultHttpClient.Create(configuration.ConnectionTimeout, configuration.Proxy, NullLogger.Instance);

            var requestExecutor = new DefaultRequestExecutor(configuration, httpClient, NullLogger.Instance);
            var resourceFactory = new ResourceFactory(testOktaClient, NullLogger.Instance);
            var dataStore       = new DefaultDataStore(requestExecutor, new DefaultSerializer(), resourceFactory, NullLogger.Instance);

            var response = await dataStore.GetAsync <Resource>(
                new HttpRequest
            {
                // Endpoint that returns a chunked response
                Uri = $"/api/v1/meta/schemas/user/default",
            },
                null,
                default(CancellationToken));

            var connectionHeader = response.Headers.FirstOrDefault(x => x.Key.Equals("connection", StringComparison.OrdinalIgnoreCase));

            connectionHeader.Value?.Any(s => s.Equals("close", StringComparison.OrdinalIgnoreCase)).Should().BeFalse();
        }