Beispiel #1
0
        public async Task ThrowApiExceptionWhenContentTypeIsJsonAndErrorDescriptionIsNotPresent()
        {
            var response = @"
                            {
                                ""errorCode"": ""E0000022"",
                                ""errorSummary"": ""The endpoint does not support the provided HTTP method"",
                                ""errorLink"": ""E0000022"",
                                ""errorId"": ""oaey24IvG9nTJCx8gqGeh8j4Q"",
                                ""errorCauses"": []
                            }";

            var mockResponseHeaders = new List <KeyValuePair <string, IEnumerable <string> > >();

            mockResponseHeaders.Add(new KeyValuePair <string, IEnumerable <string> >("content-type", new List <string>()
            {
                "Accept: application/json"
            }));

            var mockRequestExecutor = new MockedStringRequestExecutor(response, statusCode: 400, mockResponseHeaders);

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

            OktaException exception = await Assert.ThrowsAsync <OktaApiException>(
                () => dataStore.PostAsync <TestResource>(request, new RequestContext(), CancellationToken.None));

            exception.Message.Should().Contain("The endpoint does not support the provided HTTP method");
        }
Beispiel #2
0
        public async Task ThrowOAuthExceptionWhenErrorDescriptionIsPresent()
        {
            var response = @"
                            {
                                ""error"": ""invalid_grant"",
                                ""error_description"": ""The interaction code is invalid or has expired.""
                            }";

            var mockResponseHeaders = new List <KeyValuePair <string, IEnumerable <string> > >();

            mockResponseHeaders.Add(new KeyValuePair <string, IEnumerable <string> >("content-type", new List <string>()
            {
                "Accept: application/json"
            }));

            var mockRequestExecutor = new MockedStringRequestExecutor(response, statusCode: 400, mockResponseHeaders);

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

            OktaException exception = await Assert.ThrowsAsync <OktaOAuthException>(
                () => dataStore.PostAsync <TestResource>(request, new RequestContext(), CancellationToken.None));

            exception.Message.Should().Contain("The interaction code is invalid or has expired.");
        }
Beispiel #3
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, null), NullLogger.Instance, new UserAgentBuilder("test", UserAgentHelper.SdkVersion));
            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();
        }
Beispiel #4
0
        public async Task ThrowIonApiExceptionWhenContentTypeIsJsonIon()
        {
            var response = @"{
                              ""version"": ""1.0.0"",
                              ""messages"": {
                                ""type"": ""array"",
                                ""value"": [
                                  {
                                    ""message"": ""'stateHandle' is required."",
                                    ""i18n"": {
                                      ""key"": ""api.error.field_required"",
                                      ""params"": [
                                        ""stateHandle""
                                      ]
                                    },
                                    ""class"": ""ERROR""
                                  }
                                ]
                              }
                            }";

            var mockResponseHeaders = new List <KeyValuePair <string, IEnumerable <string> > >();

            mockResponseHeaders.Add(new KeyValuePair <string, IEnumerable <string> >("content-type", new List <string>()
            {
                "Accept: application/ion+json; okta-version=1.0.0"
            }));

            var mockRequestExecutor = new MockedStringRequestExecutor(response, statusCode: 400, mockResponseHeaders);

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

            await Assert.ThrowsAsync <OktaIonApiException>(
                () => dataStore.PostAsync <TestResource>(request, new RequestContext(), CancellationToken.None));
        }