示例#1
0
        public void StreamMethodPullWorks()
        {
            var input   = typeof(IRestMethodInfoTests);
            var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "PullStreamMethod"));

            Assert.Equal("id", fixture.ParameterMap[0]);
            Assert.Empty(fixture.QueryParameterMap);
            Assert.Equal(BodySerializationMethod.Json, fixture.BodyParameterInfo.Item1);
            Assert.True(fixture.BodyParameterInfo.Item2);
            Assert.Equal(1, fixture.BodyParameterInfo.Item3);

            Assert.Equal(typeof(bool), fixture.SerializedReturnType);
        }
示例#2
0
        public void MissingParametersShouldBlowUp()
        {
            bool shouldDie = true;

            try {
                var input   = typeof(IRestMethodInfoTests);
                var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "FetchSomeStuffMissingParameters"));
            } catch (ArgumentException) {
                shouldDie = false;
            }

            Assert.IsFalse(shouldDie);
        }
示例#3
0
        public void ValueTypesDontBlowUp()
        {
            var input   = typeof(IRestMethodInfoTests);
            var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "OhYeahValueTypes"));

            Assert.Equal("id", fixture.ParameterMap[0]);
            Assert.Equal(0, fixture.QueryParameterMap.Count);
            Assert.Equal(BodySerializationMethod.Json, fixture.BodyParameterInfo.Item1);
            Assert.False(fixture.BodyParameterInfo.Item2);
            Assert.Equal(1, fixture.BodyParameterInfo.Item3);

            Assert.Equal(typeof(bool), fixture.SerializedReturnType);
        }
示例#4
0
        public void GarbagePathsShouldThrow()
        {
            bool shouldDie = true;

            try {
                var input   = typeof(IRestMethodInfoTests);
                var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "GarbagePath"));
            } catch (ArgumentException) {
                shouldDie = false;
            }

            Assert.False(shouldDie);
        }
示例#5
0
        public void ValueTypesDontBlowUpUnBuffered()
        {
            var input   = typeof(IRestMethodInfoTests);
            var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "OhYeahValueTypesUnbuffered"));

            Assert.Equal("id", fixture.ParameterMap[0]);
            Assert.Empty(fixture.QueryParameterMap);
            Assert.Equal(BodySerializationMethod.Default, fixture.BodyParameterInfo.Item1);
            Assert.False(fixture.BodyParameterInfo.Item2); // unbuffered specified
            Assert.Equal(1, fixture.BodyParameterInfo.Item3);

            Assert.Equal(typeof(bool), fixture.SerializedReturnType);
        }
示例#6
0
        public void SyncMethodsShouldThrow()
        {
            bool shouldDie = true;

            try {
                var input   = typeof(IRestMethodInfoTests);
                var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "AsyncOnlyBuddy"));
            } catch (ArgumentException) {
                shouldDie = false;
            }

            Assert.IsFalse(shouldDie);
        }
示例#7
0
        public void GarbagePathsShouldThrow()
        {
            bool shouldDie = true;

            try {
                var input = typeof(IRestMethodInfoTests);
                var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "GarbagePath"));
            } catch (ArgumentException) {
                shouldDie = false;
            }

            Assert.IsFalse(shouldDie);
        }
示例#8
0
        public void DynamicHeadersShouldWork()
        {
            var input   = typeof(IRestMethodInfoTests);
            var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "FetchSomeStuffWithDynamicHeader"));

            Assert.Equal("id", fixture.ParameterMap[0]);
            Assert.Empty(fixture.QueryParameterMap);
            Assert.Null(fixture.BodyParameterInfo);

            Assert.Equal("Authorization", fixture.HeaderParameterMap[1]);
            Assert.True(fixture.Headers.ContainsKey("User-Agent"), "Headers include User-Agent header");
            Assert.Equal("RefitTestClient", fixture.Headers["User-Agent"]);
            Assert.Equal(2, fixture.Headers.Count);
        }
示例#9
0
        public void HardcodedHeadersShouldWork()
        {
            var input   = typeof(IRestMethodInfoTests);
            var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "FetchSomeStuffWithHardcodedHeaders"));

            Assert.Equal("id", fixture.ParameterMap[0]);
            Assert.Equal(0, fixture.QueryParameterMap.Count);
            Assert.Null(fixture.BodyParameterInfo);

            Assert.True(fixture.Headers.ContainsKey("Api-Version"), "Headers include Api-Version header");
            Assert.Equal("2", fixture.Headers["Api-Version"]);
            Assert.True(fixture.Headers.ContainsKey("User-Agent"), "Headers include User-Agent header");
            Assert.Equal("RefitTestClient", fixture.Headers["User-Agent"]);
            Assert.Equal(2, fixture.Headers.Count);
        }
示例#10
0
        public async Task MultipartUploadShouldWorkWithStreamAndCustomBoundary()
        {
            var handler = new MockHttpMessageHandler
            {
                Asserts = async content =>
                {
                    var parts = content.ToList();

                    Assert.Single(parts);

                    Assert.Equal("stream", parts[0].Headers.ContentDisposition.Name);
                    Assert.Equal("stream", parts[0].Headers.ContentDisposition.FileName);

                    using (var str = await parts[0].ReadAsStreamAsync())
                        using (var src = GetTestFileStream("Test Files/Test.pdf"))
                        {
                            Assert.True(StreamsEqual(src, str));
                        }
                }
            };

            var settings = new RefitSettings()
            {
                HttpMessageHandlerFactory = () => handler
            };

            using (var stream = GetTestFileStream("Test Files/Test.pdf"))
            {
                var fixture = RestService.For <IRunscopeApi>(BaseAddress, settings);
                var result  = await fixture.UploadStreamWithCustomBoundary(stream);
            }

            var input         = typeof(IRunscopeApi);
            var methodFixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "UploadStreamWithCustomBoundary"));

            Assert.Equal("-----SomeCustomBoundary", methodFixture.MultipartBoundary);
        }
示例#11
0
        public void TooManyComplexTypesThrows()
        {
            var input = typeof(IRestMethodInfoTests);

            Assert.Throws<ArgumentException>(() => {
                var fixture = new RestMethodInfo(
                    input, 
                    input.GetMethods().First(x => x.Name == "TooManyComplexTypes"));
            });

        }
示例#12
0
        public void UsingThePatchAttributeSetsTheCorrectMethod()
        {
            var input = typeof(IRestMethodInfoTests);
            var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "PatchSomething"));

            Assert.AreEqual("PATCH", fixture.HttpMethod.Method);
        }
示例#13
0
        public void ValueTypesDontBlowUp()
        {
            var input = typeof(IRestMethodInfoTests);
            var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "OhYeahValueTypes"));
            Assert.AreEqual("id", fixture.ParameterMap[0]);
            Assert.AreEqual(0, fixture.QueryParameterMap.Count);
            Assert.AreEqual(BodySerializationMethod.Json, fixture.BodyParameterInfo.Item1);
            Assert.AreEqual(1, fixture.BodyParameterInfo.Item2);

            Assert.AreEqual(typeof(bool), fixture.SerializedReturnType);
        }
示例#14
0
 public object BuildRequest(RestMethodInfo methodInfo, string url)
 {
     throw new System.NotImplementedException();
 }
示例#15
0
        public void ReturningTaskShouldWork()
        {
            var input = typeof(IRestMethodInfoTests);
            var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "VoidPost"));
            Assert.AreEqual("id", fixture.ParameterMap[0]);

            Assert.AreEqual(typeof(Task), fixture.ReturnType);
            Assert.AreEqual(typeof(void), fixture.SerializedReturnType);
        }
示例#16
0
        public void MissingParametersShouldBlowUp()
        {
            bool shouldDie = true;

            try {
                var input = typeof(IRestMethodInfoTests);
                var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "FetchSomeStuffMissingParameters"));
            } catch (ArgumentException) {
                shouldDie = false;
            }

            Assert.IsFalse(shouldDie);
        }
示例#17
0
        public void FindTheBodyParameter()
        {
            var input = typeof(IRestMethodInfoTests);
            var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "FetchSomeStuffWithBody"));
            Assert.AreEqual("id", fixture.ParameterMap[0]);

            Assert.IsNotNull(fixture.BodyParameterInfo);
            Assert.AreEqual(0, fixture.QueryParameterMap.Count);
            Assert.AreEqual(1, fixture.BodyParameterInfo.Item2);
        }
示例#18
0
        public object BuildRequest(RestMethodInfo methodInfo, string url)
        {
            WWW www = new WWW(url);

            return(www);
        }
示例#19
0
        public void SyncMethodsShouldThrow()
        {
            bool shouldDie = true;

            try {
                var input = typeof(IRestMethodInfoTests);
                var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "AsyncOnlyBuddy"));
            } catch (ArgumentException) {
                shouldDie = false;
            }

            Assert.False(shouldDie);
        }
示例#20
0
        public void DynamicHeadersShouldWork()
        {
            var input = typeof(IRestMethodInfoTests);
            var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "FetchSomeStuffWithDynamicHeader"));
            Assert.Equal("id", fixture.ParameterMap[0]);
            Assert.Equal(0, fixture.QueryParameterMap.Count);
            Assert.Null(fixture.BodyParameterInfo);

            Assert.Equal("Authorization", fixture.HeaderParameterMap[1]);
            Assert.True(fixture.Headers.ContainsKey("User-Agent"), "Headers include User-Agent header");
            Assert.Equal("RefitTestClient", fixture.Headers["User-Agent"]);
            Assert.Equal(2, fixture.Headers.Count);
        }
示例#21
0
 public void ParameterMappingWithHardcodedQuerySmokeTest()
 {
     var input = typeof(IRestMethodInfoTests);
     var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "FetchSomeStuffWithHardcodedQueryParam"));
     Assert.Equal("id", fixture.ParameterMap[0]);
     Assert.Equal(0, fixture.QueryParameterMap.Count);
     Assert.Null(fixture.BodyParameterInfo);
 }
示例#22
0
        public void DefaultBodyParameterDetectedForPatch()
        {
            var input = typeof(IRestMethodInfoTests);
            var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "PatchWithBodyDetected"));

            Assert.Equal(0, fixture.QueryParameterMap.Count);
            Assert.NotNull(fixture.BodyParameterInfo);
        }
示例#23
0
        public object RxBuildRequest <T>(UniRx.IObserver <T> o, Converter.Converter convert, RestMethodInfo methodInfo, string url)
        {
            Action <HttpResponseMessage <string> > responseMessage = message =>
            {
                string errorMessage = "";
                if (IsRequestError(message, out errorMessage))
                {
                    o.OnError(new Exception(errorMessage));
                    return;
                }
                string result = GetSuccessResponse(message);
                //                        result = "[]";
                //                        result = "[asd..s]";
                if (EnableDebug)
                {
                    Debug.LogFormat("Raw Response:{0}", result);
                }
                //Parse Json By Type
                if (typeof(T) == typeof(string))
                {
                    var resultData = (T)(object)result;
                    o.OnNext(resultData);
                    o.OnCompleted();
                    return;
                }
                T    data        = default(T);
                bool formatError = false;
                try
                {
                    data = convert.FromBody <T>(result);
                }
                catch (ConversionException e)
                {
                    formatError = true;
                    o.OnError(new Exception(e.Message));
                }
                if (!formatError)
                {
                    o.OnNext(data);
                    o.OnCompleted();
                }
            };
            HttpClientRequest httpClientRequest = new HttpClientRequest(new Uri(url), responseMessage);

            ConfigureRESTfulApi(methodInfo, httpClientRequest);
            return(httpClientRequest);
        }
示例#24
0
 public void AliasMappingShouldWork()
 {
     var input = typeof(IRestMethodInfoTests);
     var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "FetchSomeStuffWithAlias"));
     Assert.AreEqual("id", fixture.ParameterMap[0]);
     Assert.AreEqual(0, fixture.QueryParameterMap.Count);
     Assert.IsNull(fixture.BodyParameterInfo);
 }
示例#25
0
        public void ManyComplexTypes()
        {
            var input = typeof(IRestMethodInfoTests);
            var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "ManyComplexTypes"));

            Assert.AreEqual(1, fixture.QueryParameterMap.Count);
            Assert.IsNotNull(fixture.BodyParameterInfo);
            Assert.AreEqual(1, fixture.BodyParameterInfo.Item2);
        }
示例#26
0
        public void HardcodedHeadersShouldWork()
        {
            var input = typeof(IRestMethodInfoTests);
            var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "FetchSomeStuffWithHardcodedHeaders"));
            Assert.AreEqual("id", fixture.ParameterMap[0]);
            Assert.AreEqual(0, fixture.QueryParameterMap.Count);
            Assert.IsNull(fixture.BodyParameterInfo);

            Assert.IsTrue(fixture.Headers.ContainsKey("Api-Version"), "Headers include Api-Version header");
            Assert.AreEqual("2", fixture.Headers["Api-Version"]);
            Assert.IsTrue(fixture.Headers.ContainsKey("User-Agent"), "Headers include User-Agent header");
            Assert.AreEqual("Refit Test Client", fixture.Headers["User-Agent"]);
            Assert.AreEqual(2, fixture.Headers.Count);
        }
示例#27
0
        public void DefaultBodyParameterNotDetectedForGet()
        {
            var input = typeof(IRestMethodInfoTests);
            var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "GetWithBodyDetected"));

            Assert.AreEqual(1, fixture.QueryParameterMap.Count);
            Assert.IsNull(fixture.BodyParameterInfo);
        }
示例#28
0
 public void ParameterMappingWithQuerySmokeTest()
 {
     var input = typeof(IRestMethodInfoTests);
     var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "FetchSomeStuffWithQueryParam"));
     Assert.AreEqual("id", fixture.ParameterMap[0]);
     Assert.AreEqual("search", fixture.QueryParameterMap[1]);
     Assert.IsNull(fixture.BodyParameterInfo);
 }
示例#29
0
 public void MultipleParametersPerSegmentShouldWork()
 {
     var input = typeof(IRestMethodInfoTests);
     var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "FetchAnImage"));
     Assert.AreEqual("width", fixture.ParameterMap[0]);
     Assert.AreEqual("height", fixture.ParameterMap[1]);
     Assert.AreEqual(0, fixture.QueryParameterMap.Count);
     Assert.IsNull(fixture.BodyParameterInfo);
 }
示例#30
0
        public void AllowUrlEncodedContent()
        {
            var input = typeof(IRestMethodInfoTests);
            var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "PostSomeUrlEncodedStuff"));
            Assert.AreEqual("id", fixture.ParameterMap[0]);

            Assert.IsNotNull(fixture.BodyParameterInfo);
            Assert.AreEqual(0, fixture.QueryParameterMap.Count);
            Assert.AreEqual(BodySerializationMethod.UrlEncoded, fixture.BodyParameterInfo.Item1);
        }
示例#31
0
        private static void ConfigureRESTfulApi(RestMethodInfo methodInfo, HttpClientRequest client)
        {
            //add headers
            if (methodInfo.Headers.Count > 0)
            {
                foreach (var keyValuePair in methodInfo.Headers)
                {
                    client.Request.Headers.Add(keyValuePair.Key, keyValuePair.Value);
                }
            }
            if (methodInfo.HeaderParameterMap.Count > 0)
            {
                foreach (var keyValuePair in methodInfo.HeaderParameterMap)
                {
                    client.Request.Headers.Add(keyValuePair.Key, keyValuePair.Value);
                }
            }
            HttpAction httpAction = (HttpAction)Enum.Parse(typeof(HttpAction), methodInfo.Method.ToString());

            client.SetMethod(httpAction);
            switch (httpAction)
            {
            case HttpAction.Delete:
                break;

            case HttpAction.Get:
                break;

            case HttpAction.Patch:
            case HttpAction.Post:
            case HttpAction.Put:
                MultipartFormDataContent multipartFormDataContent = new MultipartFormDataContent();
                FormUrlEncodedContent    formUrlEncodedContent    = null;
                ByteArrayContent         byteArrayContent         = null;
                if (methodInfo.IsMultipart && methodInfo.GotPart && methodInfo.Part != null)
                {
                    //multipart bytes
                    byteArrayContent = new ByteArrayContent(methodInfo.Part.GetBinaryData(), methodInfo.Part.Mimetype);
                }
                if (methodInfo.FieldParameterMap.Count > 0)
                {
                    //field
                    var fields = new Dictionary <string, string>();
                    foreach (var keyValuePair in methodInfo.FieldParameterMap)
                    {
                        fields.Add(keyValuePair.Key, keyValuePair.Value.ToString());
                    }
                    formUrlEncodedContent = new FormUrlEncodedContent(fields);
                }

                if (byteArrayContent != null)
                {
                    //multipart/form-data; boundary=***
                    multipartFormDataContent.Add(byteArrayContent, methodInfo.Part.Field, methodInfo.Part.FileName);
                    if (formUrlEncodedContent != null)
                    {
                        foreach (var keyValuePair in methodInfo.FieldParameterMap)
                        {
                            StringContent stringContent = new StringContent(keyValuePair.Value.ToString());
                            multipartFormDataContent.Add(stringContent, keyValuePair.Key);
                        }
                        client.SetHttpContent(multipartFormDataContent);
                    }
                }
                else if (formUrlEncodedContent != null)
                {
                    client.SetHttpContent(formUrlEncodedContent);
                }
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            if (!string.IsNullOrEmpty(methodInfo.bodyString))
            {
                //raw body application/json
                StringContent content = new StringContent(methodInfo.bodyString, Encoding.UTF8, "application/json");
                client.SetHttpContent(content);
            }
//          client.SetProxy(new WebProxy(new Uri("http://127.0.0.1:8888")));
        }