Example #1
0
        public async Task ShouldBeRightHttpMethod(HttpMethod httpMethod, Func <IRestClient, IRestRequest> getRestRequest)
        {
            string url         = "http://example.org";
            string relativeUrl = "/api/posts";
            var    mockHttp    = new MockHttpMessageHandler();

            mockHttp.Expect(httpMethod, url + relativeUrl)
            .Respond(HttpStatusCode.OK);

            IRestClient restClient = new SimpleRestClient();

            restClient.HttpClient             = new HttpClient(mockHttp);
            restClient.HttpClient.BaseAddress = new Uri(url);

            var restRequest = getRestRequest(restClient);

            var httpResponse = await restRequest.WithUriTemplate(relativeUrl)
                               .ReadAsHttpResponseMessageAsync();

            httpResponse.StatusCode
            .Should()
            .Be(HttpStatusCode.OK);

            mockHttp.VerifyNoOutstandingExpectation();
        }
Example #2
0
        public async Task ShouldSerializeContent()
        {
            string hostUrl     = "http://www.mocky.io";
            string relativeUrl = "/v2/5a68c5a32e0000be27d5b580";

            var testObject = new TestObject()
            {
                Value = "test"
            };

            RestSettings settings = new RestSettings();

            settings.MediaTypeFormatters.Default = new TestMediaFormatter(JsonSerializer.Create(new JsonSerializerSettings()));

            IRestClient restClient = new SimpleRestClient(settings);

            restClient.HttpClient             = new HttpClient();
            restClient.HttpClient.BaseAddress = new Uri(hostUrl);

            var restRequest = RestRequest.Post(restClient)
                              .WithUriTemplate(relativeUrl)
                              .WithContent(testObject, "testObject");

            var response = await restRequest.ReadAsObject <TestObject>();

            response.Value.ShouldBeEquivalentTo("success");
        }
Example #3
0
        public async Task ShouldBeRightUrl(string expectedUrl, string hostUrl, string uriTemplatePrefix, string uriTemplate, string uriTemplateSuffix)
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp.Expect(HttpMethod.Get, expectedUrl)
            .Respond(HttpStatusCode.OK);

            RestSettings settings = new RestSettings();

            settings.CustomParameters.Add("cversion", "v3");
            settings.CustomParameters.Add("ctest", "72");

            IRestClient restClient = new SimpleRestClient(settings);

            restClient.HttpClient             = new HttpClient(mockHttp);
            restClient.HttpClient.BaseAddress = new Uri(hostUrl);

            var restRequest = RestRequest.Get(restClient)
                              .WithUriTemplatePrefix(uriTemplatePrefix)
                              .WithUriTemplateSuffix(uriTemplateSuffix)
                              .WithUriTemplate(uriTemplate)
                              .WithUriVariable("version", "v2")
                              .WithUriVariable("test", "45");

            var httpResponse = await restRequest.ReadAsHttpResponseMessageAsync();

            httpResponse.RequestMessage
            .RequestUri
            .OriginalString
            .ShouldBeEquivalentTo(expectedUrl);

            httpResponse.StatusCode
            .Should()
            .Be(HttpStatusCode.OK);

            mockHttp.VerifyNoOutstandingExpectation();
        }