Ejemplo n.º 1
0
        public async Task GivenThatAQuizExistsPostingAnAnswerCreatesAQuizResponse()
        {
            _mockProviderService
            .Given("There is a quiz with id '123'")
            .UponReceiving("A POST request creates a quiz response")
            .With(new ProviderServiceRequest
            {
                Method  = HttpVerb.Post,
                Path    = "/api/quizzes/123/responses",
                Headers = new Dictionary <string, object>
                {
                    { "Content-Type", "application/json" }
                }
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status  = 201,
                Headers = new Dictionary <string, object>
                {
                    { "Content-Type", "application/json" },
                    { "Location", PactNet.Matchers.Match.Regex("/api/quizzes/123/responses/1", "responses\\/[0-9]*") }
                }
            });

            var consumer = new QuizClient(_mockProviderServiceBaseUri, Client);

            var result = await consumer.PostQuizResponseAsync(new QuestionResponse(), 123);

            Assert.True(string.IsNullOrEmpty(result.ErrorMessage), result.ErrorMessage);
            Assert.Equal(HttpStatusCode.Created, result.StatusCode);
            Assert.NotNull(result.Value);

            _mockProviderService.VerifyInteractions();
        }
Ejemplo n.º 2
0
        public async Task PutQuestion_WhenAQuestionWExists_UpdatesTheQuestion()
        {
            _mockProviderService
            .Given("There are some quizzes")
            .UponReceiving("A PUT request to update a quiz question with id = 1")
            .With(new ProviderServiceRequest
            {
                Method  = HttpVerb.Put,
                Path    = "/api/quizzes/123/questions/1",
                Headers = new Dictionary <string, object>
                {
                    { "Content-Type", "application/json" }
                }
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status  = 204,
                Headers = new Dictionary <string, object>
                {
                    { "Content-Type", "application/json; charset=utf-8" }
                }
            });

            var consumer = new QuizClient(_mockProviderServiceBaseUri, Client);

            var result = await consumer.PutQuestionAsync(123, 1, new QuizQuestion { Text = "Updated text" }, CancellationToken.None);

            Assert.True(string.IsNullOrEmpty(result.ErrorMessage), result.ErrorMessage);
            Assert.Equal(HttpStatusCode.NoContent, result.StatusCode);
            Assert.NotEqual(Quiz.NotFound, result.Value);

            _mockProviderService.VerifyInteractions();
        }
        public async Task GetQuiz_WhenAQuizWExists_ReturnsTheQuiz()
        {
            _mockProviderService
            .Given("There are some quizzes")
            .UponReceiving("A GET request to retrieve the quiz")
            .With(new ProviderServiceRequest {
                Method = HttpVerb.Get, Path = "/api/quizzes/123", Headers = new Dictionary <string, object> {
                    { "Accept", "application/json" }
                }
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status  = 200,
                Headers = new Dictionary <string, object> {
                    { "Content-Type", "application/json; charset=utf-8" }
                },
                Body = new { id = 123, title = "This is quiz 123" }
            });

            var consumer = new QuizClient(_mockProviderServiceBaseUri, Client);

            var result = await consumer.GetQuizAsync(123, CancellationToken.None);

            Assert.True(string.IsNullOrEmpty(result.ErrorMessage), result.ErrorMessage);
            Assert.Equal(HttpStatusCode.OK, result.StatusCode);
            Assert.NotEqual(Quiz.NotFound, result.Value);
            Assert.Equal("This is quiz 123", result.Value.Title);

            _mockProviderService.VerifyInteractions();
        }
Ejemplo n.º 4
0
        public async Task PostQuestion_Returns201CreatedAndLocationHeader()
        {
            _mockProviderService
            .Given("There are some quizzes")
            .UponReceiving("A POST request to quiz 123 questions collection")
            .With(new ProviderServiceRequest
            {
                Method  = HttpVerb.Post,
                Path    = "/api/quizzes/123/questions",
                Headers = new Dictionary <string, object>
                {
                    { "Content-Type", "application/json" }
                }
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status  = 201,
                Headers = new Dictionary <string, object>
                {
                    { "Content-Type", "application/json" },
                    { "Location", PactNet.Matchers.Match.Regex("/api/quizzes/123/questions/1", "quizzes\\/123\\/questions\\/[0-9]*") }
                }
            });

            var consumer = new QuizClient(_mockProviderServiceBaseUri, Client);

            var result = await consumer.PostQuestionAsync(123, new QuizQuestion { Text = "This is a question" }, CancellationToken.None);

            Assert.True(string.IsNullOrEmpty(result.ErrorMessage), result.ErrorMessage);
            Assert.Equal(HttpStatusCode.Created, result.StatusCode);
            Assert.NotNull(result.Value);

            _mockProviderService.VerifyInteractions();
        }
Ejemplo n.º 5
0
 public ApiClient()
 {
     QuestionsService = new QuestionsClient("https://localhost:44353/", httpClient);
     QuizService      = new QuizClient("https://localhost:44353/", httpClient);
     StudentService   = new StudentsClient("https://localhost:44353/", httpClient);
     GradesService    = new GradesClient("https://localhost:44353/", httpClient);
 }