From 8db866b9c60eaf7126a4889eefaa257654d8cfbd Mon Sep 17 00:00:00 2001 From: Felipe Baltazar Date: Sat, 9 Feb 2019 00:26:54 -0200 Subject: [PATCH 1/2] Options Attribute - `OptionsAttribute` has been created; - Test **HttpMethod** name for `OptionsAtrtibute`; --- Refit.Tests/RequestBuilder.cs | 19 ++++++++++++++++++- Refit/Attributes.cs | 13 ++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Refit.Tests/RequestBuilder.cs b/Refit.Tests/RequestBuilder.cs index 66d3e55c0..3e27714d4 100644 --- a/Refit.Tests/RequestBuilder.cs +++ b/Refit.Tests/RequestBuilder.cs @@ -68,6 +68,9 @@ public interface IRestMethodInfoTests [Patch("/foo/{id}")] IObservable PatchSomething(int id, [Body] string someAttribute); + [Options("/foo/{id}")] + Task SendOptions(int id, [Body] string someAttribute); + [Post("/foo/{id}")] Task> PostReturnsApiResponse(int id); @@ -86,6 +89,9 @@ public interface IRestMethodInfoTests [Patch("/foo")] Task PatchWithBodyDetected(Dictionary theData); + [Options("/foo")] + Task OptionsWithBodyDetected(Dictionary theData); + [Post("/foo")] Task TooManyComplexTypes(Dictionary theData, Dictionary theData1); @@ -383,6 +389,15 @@ public void UsingThePatchAttributeSetsTheCorrectMethod() Assert.Equal("PATCH", fixture.HttpMethod.Method); } + [Fact] + public void UsingOptionsAttribute() + { + var input = typeof(IRestMethodInfoTests); + var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == nameof(IDummyHttpApi.SendOptions))); + + Assert.Equal("OPTIONS", fixture.HttpMethod.Method); + } + [Fact] public void ApiResponseShouldBeSet() { @@ -478,6 +493,9 @@ public interface IDummyHttpApi [Patch("/foo/bar/{id}")] IObservable PatchSomething(int id, [Body] string someAttribute); + [Options("/foo/bar/{id}")] + Task SendOptions(int id, [Body] string someAttribute); + [Get("/foo/bar/{id}")] Task FetchSomeStuffWithQueryFormat([Query(Format = "0.0")] int id); @@ -1271,7 +1289,6 @@ public void ICanPostAValueTypeIfIWantYoureNotTheBossOfMe() var expected = string.Format("\"{0}\"", guid); var output = factory(new object[] { 7, guid }); - Assert.Equal(expected, output.SendContent); } } diff --git a/Refit/Attributes.cs b/Refit/Attributes.cs index 34c7e121b..389308992 100644 --- a/Refit/Attributes.cs +++ b/Refit/Attributes.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; @@ -79,6 +79,17 @@ public override HttpMethod Method } } + [AttributeUsage(AttributeTargets.Method)] + public class OptionsAttribute : HttpMethodAttribute + { + public OptionsAttribute(string path) : base(path) { } + + public override HttpMethod Method + { + get { return new HttpMethod("OPTIONS"); } + } + } + [AttributeUsage(AttributeTargets.Method)] public class HeadAttribute : HttpMethodAttribute { From b44e75a8e3dbd020672be5ff33479ca479075bd5 Mon Sep 17 00:00:00 2001 From: Felipe Baltazar Date: Sat, 9 Feb 2019 00:47:24 -0200 Subject: [PATCH 2/2] `OptionsWithBodyDetected` removed --- InterfaceStubGenerator.Core/InterfaceStubGenerator.cs | 2 +- Refit.Tests/RequestBuilder.cs | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/InterfaceStubGenerator.Core/InterfaceStubGenerator.cs b/InterfaceStubGenerator.Core/InterfaceStubGenerator.cs index 0524701ae..c63900ee6 100644 --- a/InterfaceStubGenerator.Core/InterfaceStubGenerator.cs +++ b/InterfaceStubGenerator.Core/InterfaceStubGenerator.cs @@ -25,7 +25,7 @@ namespace Refit.Generator public class InterfaceStubGenerator { static readonly HashSet httpMethodAttributeNames = new HashSet( - new[] { "Get", "Head", "Post", "Put", "Delete", "Patch" } + new[] { "Get", "Head", "Post", "Put", "Delete", "Patch", "Options" } .SelectMany(x => new[] { "{0}", "{0}Attribute" }.Select(f => string.Format(f, x)))); public InterfaceStubGenerator() : this(null, null) { } diff --git a/Refit.Tests/RequestBuilder.cs b/Refit.Tests/RequestBuilder.cs index 3e27714d4..fc12ab710 100644 --- a/Refit.Tests/RequestBuilder.cs +++ b/Refit.Tests/RequestBuilder.cs @@ -89,9 +89,6 @@ public interface IRestMethodInfoTests [Patch("/foo")] Task PatchWithBodyDetected(Dictionary theData); - [Options("/foo")] - Task OptionsWithBodyDetected(Dictionary theData); - [Post("/foo")] Task TooManyComplexTypes(Dictionary theData, Dictionary theData1);