Example #1
0
        public static IRequestBuilder With(this IRequestBuilder builder, RequestParameterType type, string name, object value)
        {
            IRequestParameter parameter;

            if (type == RequestParameterType.Body)
            {
                parameter = new RequestBody(name, value);
            }
            else if (type == RequestParameterType.File)
            {
                if (!(value is Stream))
                {
                    throw new ArgumentException();
                }

                parameter = new RequestFile(name, name, (Stream)value);
            }
            else
            {
                if (String.IsNullOrEmpty(name))
                {
                    throw new ArgumentNullException(nameof(name));
                }

                parameter = new SimpleRequestParameter(type, name, value);
            }

            return(builder.With(parameter));
        }
Example #2
0
        public void SendTwoFiles()
        {
            var httpClient = new Mock <IHttpClient>()
                             .SetupAllProperties();

            httpClient.Setup(
                x =>
                x.SendAsync(It.Is <HttpRequestMessage>(message => message.RequestUri.ToString() == "http://example.com/foo"),
                            CancellationToken.None))
            .Returns((HttpRequestMessage message, CancellationToken token) => {
                Assert.IsInstanceOf <MultipartContent>(message.Content);
                var multiParts = (MultipartContent)message.Content;
                Assert.AreEqual(2, multiParts.Count());
                var httpResponse = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    RequestMessage = message
                };
                return(Task.FromResult(httpResponse));
            });

            var client = new RestClient(httpClient.Object, new RestClientSettings {
                BaseUri = new Uri("http://example.com")
            });

            var file1 = new RequestFile("a", "a.png", new MemoryStream(new byte[] { 0, 0, 2, 8, 9, 20 }));
            var file2 = new RequestFile("b", "b.png", new MemoryStream(new byte[] { 0, 0, 1, 87, 134, 0 }));

            var response = client.RequestAsync(builder => builder.To("foo").Post().WithFile(file1).WithFile(file2)).Result;

            Assert.IsNotNull(response);
            Assert.DoesNotThrow(() => response.AssertSuccessful());
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        }
Example #3
0
 public static RestRequest PostFile(string resource, RequestFile file, object routes = null, object query = null)
 {
     return(Build(builder => builder
                  .Post()
                  .To(resource)
                  .WithRoutes(routes)
                  .WithQueryStrings(query)
                  .With(file)));
 }
Example #4
0
 public static IRequestBodyBuilder IncludeFile(this IRequestBodyBuilder builder, RequestFile file)
 {
     return(builder.IncludePart(file));
 }
Example #5
0
 internal static HttpContent GetFileContent(this IRequestParameter parameter, IRestClient client, bool inMultipart)
 {
     return(RequestFile.CreateFileContent(client, parameter, inMultipart));
 }