public void CanBeSerialized() { var jsonSerializer = new JsonSerializer(); var context = new RestClientContext(); var json = jsonSerializer.Serialize(context); Assert.Equal("{\"CookieJar\":[]}", json); }
public async Task HostIsSetCorrectly() { var mockHttp = new MockHttpMessageHandler(); mockHttp .Expect("http://domain.ext/") .WithHeaders("host", "domain.ext") .Respond("application/json", "{'foo' : 'Bar'}"); var restClient = new RestClient(mockHttp); var context = new RestClientContext(); var request = RestRequest.Get(new RestUri("http://domain.ext/")); var response = await restClient.SendAsync <Contract>(context, request); Assert.True(response.IsSuccess); mockHttp.VerifyNoOutstandingExpectation(); }
public void CanBeSerializedWithCookies() { var timeStamp = new DateTime(1979, 12, 20, 1, 2, 3, DateTimeKind.Utc); var jsonSerializer = new JsonSerializer(); var context = new RestClientContext(); context.CookieJar.Add(new Uri("http://www.foo.ext"), SetTimestamp(new Cookie("foo1", "bar", "/", ".foo.ext"), timeStamp)); context.CookieJar.Add(new Uri("https://www.foo.ext"), SetTimestamp(new Cookie("foo2", "bar", "/", ".foo.ext") { Secure = true, Comment = "hello world", HttpOnly = true }, timeStamp)); var json = jsonSerializer.Serialize(context); Assert.Equal("{\"CookieJar\":[{\"Domain\":\".foo.ext\",\"Cookies\":[{\"Comment\":\"\",\"HttpOnly\":false,\"Discard\":false,\"Domain\":\".foo.ext\",\"Expired\":false,\"Expires\":\"0001-01-01T00:00:00\",\"Name\":\"foo1\",\"Path\":\"/\",\"Port\":\"\",\"Secure\":false,\"TimeStamp\":\"1979-12-20T01:02:03Z\",\"Value\":\"bar\",\"Version\":0}]},{\"Domain\":\".foo.ext\",\"Cookies\":[{\"Comment\":\"hello world\",\"HttpOnly\":true,\"Discard\":false,\"Domain\":\".foo.ext\",\"Expired\":false,\"Expires\":\"0001-01-01T00:00:00\",\"Name\":\"foo2\",\"Path\":\"/\",\"Port\":\"\",\"Secure\":true,\"TimeStamp\":\"1979-12-20T01:02:03Z\",\"Value\":\"bar\",\"Version\":0}]}]}", json); }
public async Task CookieIsSetCorrectly() { var mockHttp = new MockHttpMessageHandler(); mockHttp .Expect("http://domain.ext/") .WithHeaders("cookie", "foo=bar; hello=world") .Respond("application/json", "{'foo' : 'Bar'}"); var restClient = new RestClient(mockHttp); var context = new RestClientContext(); context.CookieJar.Add(new Uri("http://domain.ext/"), new Cookie("foo", "bar")); context.CookieJar.Add(new Uri("http://domain.ext/"), new Cookie("hello", "world")); var request = RestRequest.Get(new RestUri("http://domain.ext/")); var response = await restClient.SendAsync <Contract>(context, request); Assert.True(response.IsSuccess); mockHttp.VerifyNoOutstandingExpectation(); }
public async Task ContentIsHandeledCorrectly() { var json = "{'foo' : 'Bar'}"; var mockHttp = new MockHttpMessageHandler(); mockHttp .Expect("http://domain.ext/") .Respond("application/json", json); var restClient = new RestClient(mockHttp); var context = new RestClientContext(); var request = RestRequest.Get(new RestUri("http://domain.ext/")); var response = await restClient.SendAsync <Contract>(context, request); Assert.True(response.IsSuccess, "IsSuccess is false"); Assert.Equal(RestContentType.Json, response.ContentType); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(json, response.RawContent.ToString()); Assert.Equal(response.Content.Foo, "Bar"); mockHttp.VerifyNoOutstandingExpectation(); }
public async Task CookiesAreHandeledCorrectly() { var res = new HttpResponseMessage(HttpStatusCode.OK); res.Headers.TryAddWithoutValidation("set-cookie", "hello=world"); var mockHttp = new MockHttpMessageHandler(); mockHttp .Expect("http://domain.ext/") .Respond(req => res); var restClient = new RestClient(mockHttp); var context = new RestClientContext(); var request = RestRequest.Get(new RestUri("http://domain.ext/")); var response = await restClient.SendAsync <Contract>(context, request); var cookies = context.CookieJar.GetCookies(new RestUri("http://domain.ext/")); Assert.Equal(1, cookies.Count); Assert.Equal("hello", cookies[0].Name); Assert.Equal("world", cookies[0].Value); mockHttp.VerifyNoOutstandingExpectation(); }
public void CookieJarIsNotNull() { var context = new RestClientContext(); Assert.NotNull(context.CookieJar); }