├── .gitignore ├── LICENSE ├── README.md ├── RefitVsRestSharp.Server ├── Controllers │ ├── FileController.cs │ ├── ResourceController.cs │ ├── UserController.cs │ └── UserManageController.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── RefitVsRestSharp.Server.csproj ├── Startup.cs ├── Views │ └── User.cs ├── appsettings.Development.json └── appsettings.json ├── RefitVsRestSharp.Test ├── Apis │ ├── IResources.cs │ ├── IUploadFile.cs │ └── IUsers.cs ├── GetWithParameters.cs ├── HeadersUsage.cs ├── PostWithBody.cs ├── RefitVsRestSharp.Test.csproj ├── SimpleGet.cs └── UploadFile.cs └── RefitVsRestSharp.sln /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.*~ 3 | project.lock.json 4 | .DS_Store 5 | *.pyc 6 | nupkg/ 7 | 8 | # Visual Studio Code 9 | .vscode 10 | 11 | # User-specific files 12 | *.suo 13 | *.user 14 | *.userosscache 15 | *.sln.docstates 16 | 17 | # Build results 18 | [Dd]ebug/ 19 | [Dd]ebugPublic/ 20 | [Rr]elease/ 21 | [Rr]eleases/ 22 | x64/ 23 | x86/ 24 | build/ 25 | bld/ 26 | [Bb]in/ 27 | [Oo]bj/ 28 | [Oo]ut/ 29 | msbuild.log 30 | msbuild.err 31 | msbuild.wrn 32 | 33 | # Visual Studio 2015 34 | .vs/ 35 | .idea/ 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Sebastian 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Refit vs RestSharp 2 | ## Comparison of basic use cases 3 | 4 | [Refit](https://github.com/reactiveui/refit) 5 | 6 | [RestSharp](http://restsharp.org/) 7 | 8 | 9 | #### 1. Simple GET 10 | [https://github.com/beryldev/refit-vs-restsharp/blob/master/RefitVsRestSharp.Test/SimpleGet.cs](https://github.com/beryldev/refit-vs-restsharp/blob/master/RefitVsRestSharp.Test/SimpleGet.cs) 11 | 12 | #### 2. GET with parameters 13 | [https://github.com/beryldev/refit-vs-restsharp/blob/master/RefitVsRestSharp.Test/GetWithParameters.cs](https://github.com/beryldev/refit-vs-restsharp/blob/master/RefitVsRestSharp.Test/GetWithParameters.cs) 14 | 15 | #### 3. POST with body 16 | [https://github.com/beryldev/refit-vs-restsharp/blob/master/RefitVsRestSharp.Test/PostWithBody.cs](https://github.com/beryldev/refit-vs-restsharp/blob/master/RefitVsRestSharp.Test/PostWithBody.cs) 17 | 18 | #### 4. File upload 19 | [https://github.com/beryldev/refit-vs-restsharp/blob/master/RefitVsRestSharp.Test/UploadFile.cs](https://github.com/beryldev/refit-vs-restsharp/blob/master/RefitVsRestSharp.Test/UploadFile.cs) 20 | 21 | #### 5. Usage of headers 22 | [https://github.com/beryldev/refit-vs-restsharp/blob/master/RefitVsRestSharp.Test/HeadersUsage.cs](https://github.com/beryldev/refit-vs-restsharp/blob/master/RefitVsRestSharp.Test/HeadersUsage.cs) -------------------------------------------------------------------------------- /RefitVsRestSharp.Server/Controllers/FileController.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using System.Text; 3 | using Microsoft.AspNetCore.Http; 4 | using Microsoft.AspNetCore.Mvc; 5 | using Microsoft.Extensions.Logging; 6 | 7 | namespace RefitVsRestSharp.Server.Controllers 8 | { 9 | [Controller] 10 | public class FileController : Controller 11 | { 12 | private readonly ILogger _logger; 13 | 14 | public FileController(ILogger logger) 15 | { 16 | _logger = logger; 17 | } 18 | 19 | [HttpPost("/files/upload")] 20 | public string Upload(IFormFile file) 21 | { 22 | var stream = new MemoryStream(); 23 | file.CopyTo(stream); 24 | 25 | string fileContent = Encoding.UTF8.GetString(stream.ToArray()); 26 | string filename = file.FileName; 27 | 28 | _logger.LogInformation($"File content: {fileContent}"); 29 | 30 | return $"{filename}:{fileContent}"; 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /RefitVsRestSharp.Server/Controllers/ResourceController.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using Microsoft.AspNetCore.Mvc; 3 | using Microsoft.Extensions.Logging; 4 | using Microsoft.Extensions.Primitives; 5 | 6 | namespace RefitVsRestSharp.Server.Controllers 7 | { 8 | [Route("api/resources")] 9 | [ApiController] 10 | public class ResourceController : ControllerBase 11 | { 12 | private readonly ILogger _logger; 13 | 14 | public ResourceController(ILogger logger) 15 | { 16 | _logger = logger; 17 | } 18 | 19 | [HttpGet] 20 | public ActionResult GetResources() 21 | { 22 | var result = string.Empty; 23 | if (Request.Headers.TryGetValue("x-test", out StringValues values)) 24 | { 25 | string value = values.FirstOrDefault(); 26 | result = $"x-test:{value}"; 27 | } 28 | 29 | _logger.LogInformation($"HEADER {result}"); 30 | return result; 31 | } 32 | 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /RefitVsRestSharp.Server/Controllers/UserController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.IO; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | using Microsoft.Extensions.Logging; 7 | using RefitVsRestSharp.Server.Views; 8 | 9 | namespace RefitVsRestSharp.Server.Controllers 10 | { 11 | [Route("api/users")] 12 | [ApiController] 13 | public class UserController : ControllerBase 14 | { 15 | private readonly ILogger _logger; 16 | private static IEnumerable Users => new[] 17 | { 18 | new User 19 | { 20 | Name = "username", 21 | Description = "Sample user", 22 | Roles = new[] {"RoleA", "RoleB"} 23 | } 24 | }; 25 | 26 | public UserController(ILogger logger) 27 | { 28 | _logger = logger; 29 | } 30 | 31 | [HttpGet] 32 | public ActionResult> Search(string role = "") 33 | { 34 | return string.IsNullOrEmpty(role) ? Users.ToList() 35 | : Users.Where(u => u.Roles.Contains(role)).ToList(); 36 | } 37 | 38 | [HttpGet("{username}")] 39 | public ActionResult Get(string username) 40 | { 41 | return new User 42 | { 43 | Name = username, 44 | Description = "Sample user", 45 | Roles = new []{"RoleA", "RoleB"} 46 | }; 47 | } 48 | 49 | [HttpPost] 50 | public async Task Create() 51 | { 52 | using var sr = new StreamReader(Request.Body); 53 | string body = await sr.ReadToEndAsync(); 54 | _logger.LogInformation($"Request body: {body}"); 55 | return body; 56 | } 57 | } 58 | } -------------------------------------------------------------------------------- /RefitVsRestSharp.Server/Controllers/UserManageController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | using RefitVsRestSharp.Server.Views; 3 | 4 | namespace RefitVsRestSharp.Server.Controllers 5 | { 6 | [Controller] 7 | public class UserManageController : Controller 8 | { 9 | [HttpPost("/users/new")] 10 | public JsonResult Create(User user) 11 | { 12 | JsonResult json = Json(user); 13 | return json; 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /RefitVsRestSharp.Server/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Hosting; 2 | using Microsoft.Extensions.Hosting; 3 | 4 | namespace RefitVsRestSharp.Server 5 | { 6 | public class Program 7 | { 8 | public static void Main(string[] args) 9 | { 10 | CreateHostBuilder(args).Build().Run(); 11 | } 12 | 13 | public static IHostBuilder CreateHostBuilder(string[] args) => 14 | Host.CreateDefaultBuilder(args) 15 | .ConfigureWebHostDefaults(webBuilder => 16 | { 17 | webBuilder.UseStartup(); 18 | }); 19 | } 20 | } -------------------------------------------------------------------------------- /RefitVsRestSharp.Server/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json.schemastore.org/launchsettings.json", 3 | "iisSettings": { 4 | "windowsAuthentication": false, 5 | "anonymousAuthentication": true, 6 | "iisExpress": { 7 | "applicationUrl": "http://localhost:47497", 8 | "sslPort": 44371 9 | } 10 | }, 11 | "profiles": { 12 | "IIS Express": { 13 | "commandName": "IISExpress", 14 | "launchBrowser": true, 15 | "launchUrl": "", 16 | "environmentVariables": { 17 | "ASPNETCORE_ENVIRONMENT": "Development" 18 | } 19 | }, 20 | "RefitCompare.Server": { 21 | "commandName": "Project", 22 | "launchBrowser": true, 23 | "launchUrl": "", 24 | "applicationUrl": "https://localhost:5001;http://localhost:5000", 25 | "environmentVariables": { 26 | "ASPNETCORE_ENVIRONMENT": "Development" 27 | } 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /RefitVsRestSharp.Server/RefitVsRestSharp.Server.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /RefitVsRestSharp.Server/Startup.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Builder; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.AspNetCore.Mvc; 4 | using Microsoft.Extensions.Configuration; 5 | using Microsoft.Extensions.DependencyInjection; 6 | using Microsoft.Extensions.Hosting; 7 | 8 | namespace RefitVsRestSharp.Server 9 | { 10 | public class Startup 11 | { 12 | public Startup(IConfiguration configuration) 13 | { 14 | Configuration = configuration; 15 | } 16 | 17 | public IConfiguration Configuration { get; } 18 | 19 | public void ConfigureServices(IServiceCollection services) 20 | { 21 | services.AddControllers() 22 | .SetCompatibilityVersion(CompatibilityVersion.Version_3_0) 23 | .AddNewtonsoftJson(); 24 | } 25 | 26 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 27 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 28 | { 29 | if (env.IsDevelopment()) 30 | { 31 | app.UseDeveloperExceptionPage(); 32 | } 33 | else 34 | { 35 | app.UseHsts(); 36 | } 37 | 38 | app.UseRouting(); 39 | 40 | app.UseEndpoints(endpoints => 41 | { 42 | endpoints.MapControllers(); 43 | }); 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /RefitVsRestSharp.Server/Views/User.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace RefitVsRestSharp.Server.Views 4 | { 5 | public class User 6 | { 7 | public string Name { get; set; } 8 | public string Description { get; set; } 9 | public IEnumerable Roles { get; set; } //// RestSharp deserialization fails when array 10 | } 11 | } -------------------------------------------------------------------------------- /RefitVsRestSharp.Server/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Debug", 5 | "System": "Information", 6 | "Microsoft": "Information" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /RefitVsRestSharp.Server/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Warning" 5 | } 6 | }, 7 | "AllowedHosts": "*" 8 | } 9 | -------------------------------------------------------------------------------- /RefitVsRestSharp.Test/Apis/IResources.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using Refit; 3 | 4 | namespace RefitVsRestSharp.Test.Apis 5 | { 6 | public interface IResources 7 | { 8 | [Headers("x-test: header value")] 9 | [Get("/api/resources")] 10 | Task Get(); 11 | 12 | [Get("/api/resources")] 13 | Task Get([Header("x-test")] string header); 14 | } 15 | } -------------------------------------------------------------------------------- /RefitVsRestSharp.Test/Apis/IUploadFile.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using System.Net.Http; 3 | using System.Threading.Tasks; 4 | using Refit; 5 | 6 | namespace RefitVsRestSharp.Test.Apis 7 | { 8 | interface IUploadFile 9 | { 10 | [Multipart] 11 | [Post("/files/upload")] 12 | Task Upload(byte[] file); 13 | 14 | [Multipart] 15 | [Post("/files/upload")] 16 | Task Upload(Stream file); 17 | 18 | [Multipart] 19 | [Post("/files/upload")] 20 | Task Upload(FileInfo file); 21 | 22 | [Multipart] 23 | [Post("/files/upload")] 24 | Task Upload(ByteArrayPart file); 25 | 26 | [Multipart] 27 | [Post("/files/upload")] 28 | Task Upload(StreamPart file); 29 | 30 | [Multipart] 31 | [Post("/files/upload")] 32 | Task Upload(FileInfoPart file); 33 | } 34 | } -------------------------------------------------------------------------------- /RefitVsRestSharp.Test/Apis/IUsers.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.IO; 3 | using System.Net.Http; 4 | using System.Threading.Tasks; 5 | using Refit; 6 | using RefitVsRestSharp.Server.Views; 7 | 8 | namespace RefitVsRestSharp.Test.Apis 9 | { 10 | interface IUsers 11 | { 12 | [Get("/api/users/{username}")] 13 | Task Get(string username); 14 | 15 | [Get("/api/users")] 16 | Task> Search(string role); 17 | 18 | 19 | [Post("/api/users")] 20 | Task Create([Body] User user); 21 | 22 | [Post("/api/users")] 23 | Task Create([Body]string data); 24 | 25 | [Post("/api/users")] 26 | Task Create([Body]Stream data); 27 | 28 | [Post("/users/new")] 29 | Task CreateByFromSubmit([Body(BodySerializationMethod.UrlEncoded)] User user); 30 | } 31 | } -------------------------------------------------------------------------------- /RefitVsRestSharp.Test/GetWithParameters.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using System.Threading.Tasks; 4 | using Refit; 5 | using RefitVsRestSharp.Server.Views; 6 | using RefitVsRestSharp.Test.Apis; 7 | using RestSharp; 8 | using Xunit; 9 | 10 | namespace RefitVsRestSharp.Test 11 | { 12 | public class GetWithParameters 13 | { 14 | [Fact] 15 | public async Task DoWithRestSharp() 16 | { 17 | var client = new RestClient("http://localhost:5000"); 18 | var request = new RestRequest("api/users"); 19 | request.AddParameter("role", "RoleA"); 20 | 21 | IRestResponse> response = await client.ExecuteAsync>(request); 22 | IEnumerable users = response.Data; 23 | 24 | AssertResult(users); 25 | } 26 | 27 | [Fact] 28 | public async Task DoWithRefit() 29 | { 30 | var api = RestService.For("http://localhost:5000"); 31 | 32 | IEnumerable users = await api.Search("RoleA"); 33 | 34 | AssertResult(users); 35 | } 36 | 37 | private void AssertResult(IEnumerable result) 38 | { 39 | User user = result.FirstOrDefault(); 40 | Assert.NotNull(user); 41 | Assert.Equal("username", user.Name); 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /RefitVsRestSharp.Test/HeadersUsage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Net.Http; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | using Refit; 6 | using RefitVsRestSharp.Test.Apis; 7 | using RestSharp; 8 | using Xunit; 9 | 10 | namespace RefitVsRestSharp.Test 11 | { 12 | public sealed class HeadersUsage 13 | { 14 | [Fact] 15 | public async Task DoWithRestSharp() 16 | { 17 | var client = new RestClient("http://localhost:5000"); 18 | var request = new RestRequest("api/resources"); 19 | request.AddHeader("x-test", "header value"); 20 | 21 | IRestResponse response = await client.ExecuteAsync(request); 22 | 23 | string responseContent = response.Data; 24 | Assert.Equal("x-test:header value", responseContent); 25 | } 26 | 27 | [Fact] 28 | public async Task DoWithRefitUsingStaticHeader() 29 | { 30 | var api = RestService.For("http://localhost:5000"); 31 | 32 | string response = await api.Get(); 33 | 34 | Assert.Equal("x-test:header value", response); 35 | } 36 | 37 | [Fact] 38 | public async Task DoWithRefitUsingDynamicHeader() 39 | { 40 | var api = RestService.For("http://localhost:5000"); 41 | 42 | string response = await api.Get("header value"); 43 | 44 | Assert.Equal("x-test:header value", response); 45 | } 46 | 47 | [Fact] 48 | public async Task DoWithRefitUsingHttpClientHandler() 49 | { 50 | Task GetHeaderValue() => Task.FromResult("other header value"); 51 | var handler = new CustomHttpClientHandler(GetHeaderValue); 52 | var httpClient = new HttpClient(handler) 53 | { 54 | BaseAddress = new Uri("http://localhost:5000") 55 | }; 56 | 57 | var api = RestService.For(httpClient); 58 | 59 | string response = await api.Get(); 60 | 61 | Assert.Equal("x-test:other header value", response); 62 | } 63 | 64 | 65 | class CustomHttpClientHandler : HttpClientHandler 66 | { 67 | private readonly Func> _getHeaderValue; 68 | 69 | public CustomHttpClientHandler(Func> headerValue) 70 | { 71 | _getHeaderValue = headerValue ?? throw new ArgumentNullException(nameof(headerValue)); 72 | } 73 | 74 | protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) 75 | { 76 | request.Headers.Remove("x-test"); 77 | request.Headers.Add("x-test", await _getHeaderValue()); 78 | 79 | return await base.SendAsync(request, cancellationToken); 80 | } 81 | } 82 | } 83 | } -------------------------------------------------------------------------------- /RefitVsRestSharp.Test/PostWithBody.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using System.Net.Http; 3 | using System.Text; 4 | using System.Threading.Tasks; 5 | using Refit; 6 | using RefitVsRestSharp.Server.Views; 7 | using RefitVsRestSharp.Test.Apis; 8 | using RestSharp; 9 | using Xunit; 10 | 11 | namespace RefitVsRestSharp.Test 12 | { 13 | public class PostWithBody 14 | { 15 | private const string UserName = "TestUser"; 16 | private const string Description = "Description"; 17 | 18 | [Fact] 19 | public async Task DoWithRestSharp() 20 | { 21 | var client = new RestClient("http://localhost:5000"); 22 | var request = new RestRequest("api/users", Method.POST); 23 | request.AddJsonBody(new User {Name = UserName, Description = Description}); 24 | 25 | IRestResponse response = await client.ExecuteAsync(request); 26 | 27 | AssertJsonResult(response.Data); 28 | } 29 | 30 | [Fact] 31 | public async Task DoWithRefitUsingSerializedObject() 32 | { 33 | var api = RestService.For("http://localhost:5000"); 34 | 35 | HttpResponseMessage response = await api.Create(new User {Name = UserName, Description = Description}); 36 | 37 | AssertJsonResult(await response.Content.ReadAsStringAsync()); 38 | } 39 | 40 | [Fact] 41 | public async Task DoWithRefitUsingString() 42 | { 43 | var api = RestService.For("http://localhost:5000"); 44 | 45 | HttpResponseMessage response = await api.Create("{\"Name\":\"TestUser\",\"Description\":\"Description\",\"Roles\":null}"); 46 | 47 | AssertJsonResult(await response.Content.ReadAsStringAsync()); 48 | } 49 | 50 | [Fact] 51 | public async Task DoWithRefitUsingStream() 52 | { 53 | var api = RestService.For("http://localhost:5000"); 54 | 55 | byte[] bytes = Encoding.UTF8.GetBytes("{\"Name\":\"TestUser\",\"Description\":\"Description\",\"Roles\":null}"); 56 | 57 | HttpResponseMessage response = await api.Create(new MemoryStream(bytes)); 58 | 59 | AssertJsonResult(await response.Content.ReadAsStringAsync()); 60 | } 61 | 62 | [Fact] 63 | public async Task DoWithRefitUsingFormSubmit() 64 | { 65 | var api = RestService.For("http://localhost:5000"); 66 | 67 | HttpResponseMessage response = await api.CreateByFromSubmit(new User {Name = UserName, Description = Description}); 68 | 69 | AssertJsonResult(await response.Content.ReadAsStringAsync()); 70 | } 71 | 72 | private void AssertJsonResult(string data) 73 | { 74 | var expected = "{\"Name\":\"TestUser\",\"Description\":\"Description\",\"Roles\":null}"; 75 | Assert.Equal(expected, data, true); 76 | } 77 | 78 | } 79 | } -------------------------------------------------------------------------------- /RefitVsRestSharp.Test/RefitVsRestSharp.Test.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp3.1 5 | 6 | false 7 | 8 | RefitVsRestSharp.Test 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | all 18 | runtime; build; native; contentfiles; analyzers; buildtransitive 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /RefitVsRestSharp.Test/SimpleGet.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using Refit; 3 | using RefitVsRestSharp.Server.Views; 4 | using RefitVsRestSharp.Test.Apis; 5 | using RestSharp; 6 | using Xunit; 7 | 8 | namespace RefitVsRestSharp.Test 9 | { 10 | public class SimpleGet 11 | { 12 | [Fact] 13 | public async Task DoWithRestSharp() 14 | { 15 | var client = new RestClient("http://localhost:5000"); 16 | var request = new RestRequest("api/users/{username}"); 17 | request.AddUrlSegment("username", "TestUser"); 18 | 19 | IRestResponse response = await client.ExecuteAsync(request); 20 | User user = response.Data; 21 | 22 | AssertResult(user); 23 | } 24 | 25 | [Fact] 26 | public async Task DoWithRefit() 27 | { 28 | var api = RestService.For("http://localhost:5000"); 29 | 30 | User user = await api.Get("TestUser"); 31 | 32 | AssertResult(user); 33 | } 34 | 35 | private void AssertResult(User user) 36 | { 37 | Assert.NotNull(user); 38 | Assert.Equal("TestUser", user.Name); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /RefitVsRestSharp.Test/UploadFile.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using System.Net; 3 | using System.Net.Http; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Refit; 7 | using RefitVsRestSharp.Test.Apis; 8 | using RestSharp; 9 | using Xunit; 10 | 11 | namespace RefitVsRestSharp.Test 12 | { 13 | public sealed class UploadFile 14 | { 15 | private const string FileContent = "File Content"; 16 | 17 | [Fact] 18 | public async Task DoWithRestSharp() 19 | { 20 | var client = new RestClient("http://localhost:5000"); 21 | var request = new RestRequest("files/upload", Method.POST); 22 | byte[] bytes = Encoding.UTF8.GetBytes(FileContent); 23 | request.AddFile("file", bytes, "filename.txt"); 24 | 25 | IRestResponse response = await client.ExecuteAsync(request); 26 | 27 | string responseContent = response.Data; 28 | Assert.Equal(HttpStatusCode.OK, response.StatusCode); 29 | Assert.Equal($"filename.txt:{FileContent}", responseContent); 30 | } 31 | 32 | [Fact] 33 | public async Task DoWithRefitUsingByteArray() 34 | { 35 | var api = RestService.For("http://localhost:5000"); 36 | 37 | byte[] bytes = Encoding.UTF8.GetBytes(FileContent); 38 | HttpResponseMessage response = await api.Upload(bytes); 39 | 40 | string responseContent = await response.Content.ReadAsStringAsync(); 41 | Assert.Equal($"file:{FileContent}", responseContent); 42 | } 43 | 44 | [Fact] 45 | public async Task DoWithRefitUsingStream() 46 | { 47 | var api = RestService.For("http://localhost:5000"); 48 | 49 | var stream = new MemoryStream(Encoding.UTF8.GetBytes(FileContent)); 50 | HttpResponseMessage response = await api.Upload(stream); 51 | 52 | string responseContent = await response.Content.ReadAsStringAsync(); 53 | Assert.Equal($"file:{FileContent}", responseContent); 54 | } 55 | 56 | [Fact] 57 | public async Task DoWithRefitUsingFileInfo() 58 | { 59 | using (var fileStream = new FileStream("filename.txt", FileMode.Create)) 60 | { 61 | await fileStream.WriteAsync(Encoding.UTF8.GetBytes(FileContent)); 62 | } 63 | 64 | var api = RestService.For("http://localhost:5000"); 65 | 66 | var fileInfo = new FileInfo("filename.txt"); 67 | HttpResponseMessage response = await api.Upload(fileInfo); 68 | 69 | string responseContent = await response.Content.ReadAsStringAsync(); 70 | Assert.Equal($"filename.txt:{FileContent}", responseContent); 71 | } 72 | 73 | [Fact] 74 | public async Task DoWithRefitUsingByteArrayPart() 75 | { 76 | var api = RestService.For("http://localhost:5000"); 77 | 78 | var bytes = new ByteArrayPart(Encoding.UTF8.GetBytes(FileContent), "filename.txt"); 79 | HttpResponseMessage response = await api.Upload(bytes); 80 | 81 | string responseContent = await response.Content.ReadAsStringAsync(); 82 | Assert.Equal($"filename.txt:{FileContent}", responseContent); 83 | } 84 | 85 | [Fact] 86 | public async Task DoWithRefitUsingStreamPart() 87 | { 88 | var api = RestService.For("http://localhost:5000"); 89 | 90 | var stream = new MemoryStream(Encoding.UTF8.GetBytes(FileContent)); 91 | HttpResponseMessage response = await api.Upload(new StreamPart(stream, "filename.txt")); 92 | 93 | string responseContent = await response.Content.ReadAsStringAsync(); 94 | Assert.Equal($"filename.txt:{FileContent}", responseContent); 95 | } 96 | 97 | [Fact] 98 | public async Task DoWithRefitUsingFileInfoPart() 99 | { 100 | using (var fileStream = new FileStream("filename2.txt", FileMode.Create)) 101 | { 102 | await fileStream.WriteAsync(Encoding.UTF8.GetBytes(FileContent)); 103 | } 104 | 105 | var api = RestService.For("http://localhost:5000"); 106 | 107 | var fileInfo = new FileInfoPart(new FileInfo("filename2.txt"), "filename.txt"); 108 | HttpResponseMessage response = await api.Upload(fileInfo); 109 | 110 | string responseContent = await response.Content.ReadAsStringAsync(); 111 | Assert.Equal($"filename.txt:{FileContent}", responseContent); 112 | } 113 | 114 | } 115 | } -------------------------------------------------------------------------------- /RefitVsRestSharp.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RefitVsRestSharp.Test", "RefitVsRestSharp.Test\RefitVsRestSharp.Test.csproj", "{EB77D3D5-52C2-4302-829A-1A92148E1B4D}" 4 | EndProject 5 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RefitVsRestSharp.Server", "RefitVsRestSharp.Server\RefitVsRestSharp.Server.csproj", "{3E0AA8B5-8DFF-4FF9-9A40-FFD6F49411FA}" 6 | EndProject 7 | Global 8 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 9 | Debug|Any CPU = Debug|Any CPU 10 | Release|Any CPU = Release|Any CPU 11 | EndGlobalSection 12 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 13 | {EB77D3D5-52C2-4302-829A-1A92148E1B4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 14 | {EB77D3D5-52C2-4302-829A-1A92148E1B4D}.Debug|Any CPU.Build.0 = Debug|Any CPU 15 | {EB77D3D5-52C2-4302-829A-1A92148E1B4D}.Release|Any CPU.ActiveCfg = Release|Any CPU 16 | {EB77D3D5-52C2-4302-829A-1A92148E1B4D}.Release|Any CPU.Build.0 = Release|Any CPU 17 | {3E0AA8B5-8DFF-4FF9-9A40-FFD6F49411FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 18 | {3E0AA8B5-8DFF-4FF9-9A40-FFD6F49411FA}.Debug|Any CPU.Build.0 = Debug|Any CPU 19 | {3E0AA8B5-8DFF-4FF9-9A40-FFD6F49411FA}.Release|Any CPU.ActiveCfg = Release|Any CPU 20 | {3E0AA8B5-8DFF-4FF9-9A40-FFD6F49411FA}.Release|Any CPU.Build.0 = Release|Any CPU 21 | EndGlobalSection 22 | EndGlobal 23 | --------------------------------------------------------------------------------