/// <summary>
        /// Creates a new instance of <see cref="HttpEntityRequestCallback"/>.
        /// </summary>
        /// <param name="requestBody">The object to write to the request.</param>
        /// <param name="responseType">The expected response body type.</param>
        /// <param name="messageConverters">The list of <see cref="IHttpMessageConverter"/> to use.</param>
        public HttpEntityRequestCallback(object requestBody, Type responseType, IList<IHttpMessageConverter> messageConverters) :
            base(responseType, messageConverters)
        {
            if (requestBody is HttpEntity) 
            {
                this.requestEntity = (HttpEntity)requestBody;
			}
			else 
            {
                this.requestEntity = new HttpEntity(requestBody);
			}
        }
        public void ExchangeWithHeaders()
        {
            HttpHeaders headers = new HttpHeaders();
            headers.ContentLength = 11;
            HttpEntity entity = new HttpEntity("Maryse Baia", headers);

            HttpResponseMessage<string> result = template.Exchange<string>(
                "user", HttpMethod.POST, entity);

            Assert.AreEqual("3", result.Body, "Invalid content");
            Assert.AreEqual(HttpStatusCode.Created, result.StatusCode, "Invalid status code");
            Assert.AreEqual("User id '3' created with 'Maryse Baia'", result.StatusDescription, "Invalid status description");
        }
 public void GetStringWithHeaders()
 {
     HttpEntity entity = new HttpEntity();
     entity.Headers["MyHeader"] = "MyValue";
     HttpResponseMessage<string> result = template.Exchange<string>("users", HttpMethod.GET, entity);
     Assert.AreEqual("2", result.Body, "Invalid content");
 }
        protected override void OnActivate()
        {
            base.OnActivate();

            RestTemplate template = new RestTemplate(Api.Host);
            template.MessageConverters.Add(new NJsonHttpMessageConverter());

            var body = new GetRiddlesRequest();
            HttpEntity entity = new HttpEntity(body);
            entity.Headers.Add("X-Auth-Token", _registrationService.GetToken());

            template.GetForObjectAsync<GetRiddlesResponse>("/riddles", r =>
            {
                if(r.Error == null)
                {
                    Execute.OnUIThread( () =>
                    {
                        RiddleListView view = (RiddleListView)GetView();
                        ListBox riddlesList = view.riddles;
                        foreach (var riddle in r.Response.riddles)
                        {
                            StackPanel panel = new StackPanel();
                            panel.Children.Add(new TextBlock() {Text = riddle.author});

                            Image img = new Image();
                            panel.Children.Add(img);
                            panel.Tap += OnRiddleTap;
                            panel.Tag = riddle;

                            riddlesList.Items.Add(panel);

                            DownloadImage(img, riddle.photo_url);

                        }
                    } );
                }
            });
        }
        public void Exchange() 
        {
            Uri requestUri = new Uri("http://example.com");
            HttpMethod requestMethod = HttpMethod.POST;
            Expect.Call<bool>(converter.CanRead(typeof(Version), null)).Return(true);
            MediaType textPlain = new MediaType("text", "plain");
            IList<MediaType> mediaTypes = new List<MediaType>(1);
            mediaTypes.Add(textPlain);
            Expect.Call<IList<MediaType>>(converter.SupportedMediaTypes).Return(mediaTypes);
            Expect.Call<IClientHttpRequest>(requestFactory.CreateRequest(requestUri, requestMethod)).Return(request);
            HttpHeaders requestHeaders = new HttpHeaders();
            Expect.Call<HttpHeaders>(request.Headers).Return(requestHeaders).Repeat.Any();
            string helloWorld = "Hello World";
            Expect.Call<bool>(converter.CanWrite(typeof(string), null)).Return(true);
            converter.Write(helloWorld, null, request);
            ExpectGetResponse();
            Expect.Call<bool>(errorHandler.HasError(requestUri, requestMethod, response)).Return(false);
            HttpHeaders responseHeaders = new HttpHeaders();
            responseHeaders.ContentType = textPlain;
            Expect.Call<HttpHeaders>(response.Headers).Return(responseHeaders).Repeat.Any();
            ExpectHasMessageBody(responseHeaders);
            Version expected = new Version(1, 0);
            Expect.Call<bool>(converter.CanRead(typeof(Version), textPlain)).Return(true);
            Expect.Call<Version>(converter.Read<Version>(response)).Return(expected);
            Expect.Call<HttpStatusCode>(response.StatusCode).Return(HttpStatusCode.OK);
            Expect.Call<string>(response.StatusDescription).Return("OK");

            mocks.ReplayAll();

            HttpHeaders requestMessageHeaders = new HttpHeaders();
            requestMessageHeaders.Add("MyHeader", "MyValue");
            HttpEntity requestEntity = new HttpEntity(helloWorld, requestMessageHeaders);
            HttpResponseMessage<Version> result = template.Exchange<Version>("http://example.com", HttpMethod.POST, requestEntity);
            Assert.AreEqual(expected, result.Body, "Invalid POST result");
            Assert.AreEqual(textPlain, result.Headers.ContentType, "Invalid Content-Type");
            Assert.AreEqual(textPlain.ToString(), requestHeaders.GetSingleValue("Accept"), "Invalid Accept header");
            Assert.AreEqual("MyValue", requestHeaders.Get("MyHeader"), "No custom header set");
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode, "Invalid status code");
            Assert.AreEqual("OK", result.StatusDescription, "Invalid status description");
        }
        public void PostForLocationMessageCustomHeader() 
        {
            Uri requestUri = new Uri("http://example.com");
            HttpMethod requestMethod = HttpMethod.POST;
            string helloWorld = "Hello World";
            Expect.Call<IClientHttpRequest>(requestFactory.CreateRequest(requestUri, requestMethod)).Return(request);
            Expect.Call<bool>(converter.CanWrite(typeof(string), null)).Return(true);
            HttpHeaders requestHeaders = new HttpHeaders();
            Expect.Call<HttpHeaders>(request.Headers).Return(requestHeaders).Repeat.Any();
            converter.Write(helloWorld, null, request);
            ExpectGetResponse();
            Expect.Call<bool>(errorHandler.HasError(requestUri, requestMethod, response)).Return(false);
            HttpHeaders responseHeaders = new HttpHeaders();
            Uri expected = new Uri("http://example.com/hotels");
            responseHeaders.Location = expected;
            Expect.Call<HttpHeaders>(response.Headers).Return(responseHeaders).Repeat.Any();

            mocks.ReplayAll();

            HttpHeaders entityHeaders = new HttpHeaders();
            entityHeaders.Add("MyHeader", "MyValue");
            HttpEntity entity = new HttpEntity(helloWorld, entityHeaders);

            Uri result = template.PostForLocation("http://example.com", entity);
            Assert.AreEqual(expected, result, "Invalid POST result");
            Assert.AreEqual("MyValue", requestHeaders.Get("MyHeader"), "No custom header set");
        }
        public void PostUnsupportedMediaType()
        {
            Uri requestUri = new Uri("http://example.com");
            HttpMethod requestMethod = HttpMethod.POST;
            string helloWorld = "Hello World";
            Expect.Call<IClientHttpRequest>(requestFactory.CreateRequest(requestUri, requestMethod)).Return(request);
            MediaType contentType = new MediaType("foo", "bar");
            Expect.Call<bool>(converter.CanWrite(typeof(string), contentType)).Return(false);
            HttpHeaders requestHeaders = new HttpHeaders();
            Expect.Call<HttpHeaders>(request.Headers).Return(requestHeaders).Repeat.Any();

            mocks.ReplayAll();

            HttpEntity entity = new HttpEntity(helloWorld);
            entity.Headers.ContentType = contentType;
            template.PostForLocation("http://example.com", entity);
        }
        public void PostForLocationMessageContentType()
        {
            string helloWorld = "Hello World";
            Expect.Call<IClientHttpRequest>(requestFactory.CreateRequest(new Uri("http://example.com"), HttpMethod.POST)).Return(request);
            MediaType contentType = new MediaType("text", "plain");
            Expect.Call<bool>(converter.CanWrite(typeof(string), contentType)).Return(true);
            HttpHeaders requestHeaders = new HttpHeaders();
            Expect.Call<HttpHeaders>(request.Headers).Return(requestHeaders).Repeat.Any();
            converter.Write(helloWorld, contentType, request);
            ExpectGetResponse();
            Expect.Call<bool>(errorHandler.HasError(response)).Return(false);
            HttpHeaders responseHeaders = new HttpHeaders();
            Uri expected = new Uri("http://example.com/hotels");
            responseHeaders.Location = expected;
            Expect.Call<HttpHeaders>(response.Headers).Return(responseHeaders).Repeat.Any();

            mocks.ReplayAll();

            HttpHeaders entityHeaders = new HttpHeaders();
            entityHeaders.ContentType = contentType;
            HttpEntity entity = new HttpEntity(helloWorld, entityHeaders);

            Uri result = template.PostForLocation("http://example.com", entity);
            Assert.AreEqual(expected, result, "Invalid POST result");
        }