Ejemplo n.º 1
0
        public async Task CourseGetAsyncReturnsHttpRequestExceptionWhenApiReturnsNotSuccessful()
        {
            // Arrange
            var courseGetRequest = new CourseGetRequest {
                CourseId = courseId, RunId = courseRunId
            };

            var httpResponse = new HttpResponseMessage {
                StatusCode = HttpStatusCode.BadRequest, Content = new StringContent("{}")
            };

            var fakeHttpRequestSender = A.Fake <IFakeHttpRequestSender>();

            A.CallTo(() => fakeHttpRequestSender.Send(A <HttpRequestMessage> .Ignored)).Returns(httpResponse);

            var auditService = A.Fake <IAuditService>();

            var fakeHttpMessageHandler = new FakeHttpMessageHandler(fakeHttpRequestSender);
            var httpClient             = new HttpClient(fakeHttpMessageHandler)
            {
                BaseAddress = new Uri("http://SomeDummyUrl")
            };
            var findACourseClient = new FindACourseClient(httpClient, defaultSettings, auditService, defaultLogger);

            // Act
            await Assert.ThrowsAsync <HttpRequestException>(async() => await findACourseClient.CourseGetAsync(courseGetRequest).ConfigureAwait(false)).ConfigureAwait(false);

            // Assert
            httpResponse.Dispose();
            httpClient.Dispose();
            fakeHttpMessageHandler.Dispose();
        }
        public async Task CourseGetAsyncCatchesExceptionWhenApiReturnsNotSuccessful()
        {
            // Arrange
            var courseGetRequest = new CourseGetRequest {
                CourseId = courseId, RunId = courseRunId
            };
            var fakeHttpRequestSender = A.Fake <IFakeHttpRequestSender>();

            A.CallTo(() => fakeHttpRequestSender.Send(A <HttpRequestMessage> .Ignored)).Throws <Exception>();

            var auditService = A.Fake <IAuditService>();

            using var fakeHttpMessageHandler = new FakeHttpMessageHandler(fakeHttpRequestSender);
            using var httpClient             = new HttpClient(fakeHttpMessageHandler)
                  {
                      BaseAddress = new Uri("http://SomeDummyUrl")
                  };
            var findACourseClient = new FindACourseClient(httpClient, defaultSettings, auditService, defaultLogger);

            // Act
            var result = await findACourseClient.CourseGetAsync(courseGetRequest).ConfigureAwait(false);

            // Assert
            Assert.Null(result);
        }
Ejemplo n.º 3
0
        public async Task CourseGetAsyncReturnsResponseObjectWhenApiCallIsSuccessful()
        {
            // Arrange
            var courseGetRequest = new CourseGetRequest {
                CourseId = courseId, RunId = courseRunId
            };
            var expectedResponse = new CourseRunDetailResponse
            {
                CourseRunId = courseRunId,
                CourseName  = "CourseName",
                Course      = new CourseDetailResponseCourse {
                    CourseId = courseId
                },
            };

            var httpResponse = new HttpResponseMessage {
                StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(expectedResponse))
            };

            var fakeHttpRequestSender = A.Fake <IFakeHttpRequestSender>();

            A.CallTo(() => fakeHttpRequestSender.Send(A <HttpRequestMessage> .Ignored)).Returns(httpResponse);

            var auditService = A.Fake <IAuditService>();

            var fakeHttpMessageHandler = new FakeHttpMessageHandler(fakeHttpRequestSender);
            var httpClient             = new HttpClient(fakeHttpMessageHandler)
            {
                BaseAddress = new Uri("http://SomeDummyUrl")
            };

            var findACourseClient = new FindACourseClient(httpClient, defaultSettings, auditService, defaultLogger);

            // Act
            var result = await findACourseClient.CourseGetAsync(courseGetRequest).ConfigureAwait(false);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(expectedResponse.CourseName, result.CourseName);
            Assert.Equal(expectedResponse.CourseRunId, result.CourseRunId);
            Assert.Equal(expectedResponse.Course.CourseId, result.Course.CourseId);

            httpResponse.Dispose();
            httpClient.Dispose();
            fakeHttpMessageHandler.Dispose();
        }