Skip to content

Commit

Permalink
Merge pull request #251 from tmenier/dev
Browse files Browse the repository at this point in the history
Flurl 2.5.1 merge
  • Loading branch information
tmenier authored Nov 29, 2017
2 parents ee38be7 + 1c62603 commit 67734b1
Show file tree
Hide file tree
Showing 16 changed files with 245 additions and 147 deletions.
12 changes: 12 additions & 0 deletions Test/Flurl.Test/CommonExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,17 @@ public void SplitOnFirstOccurence_works() {
var result = "hello/how/are/you".SplitOnFirstOccurence('/');
Assert.AreEqual(new[] { "hello", "how/are/you" }, result);
}

[TestCase(" \"\thi there \" \t\t ", ExpectedResult = "\thi there ")]
[TestCase(" ' hi there ' ", ExpectedResult = " hi there ")]
[TestCase(" hi there ", ExpectedResult = " hi there ")]
public string StripQuotes_works(string s) => s.StripQuotes();

[Test]
public void ToInvariantString_serializes_dates_to_iso() {
Assert.AreEqual("2017-12-01T02:34:56.7890000", new DateTime(2017, 12, 1, 2, 34, 56, 789, DateTimeKind.Unspecified).ToInvariantString());
Assert.AreEqual("2017-12-01T02:34:56.7890000Z", new DateTime(2017, 12, 1, 2, 34, 56, 789, DateTimeKind.Utc).ToInvariantString());
Assert.AreEqual("2017-12-01T02:34:56.7890000-06:00", new DateTimeOffset(2017, 12, 1, 2, 34, 56, 789, TimeSpan.FromHours(-6)).ToInvariantString());
}
}
}
37 changes: 32 additions & 5 deletions Test/Flurl.Test/Http/RealHttpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,38 @@ public class RealHttpTests
[Test]
public async Task can_download_file() {
var folder = "c:\\flurl-test-" + Guid.NewGuid(); // random so parallel tests don't trip over each other
var path = await "https://www.google.com".DownloadFileAsync(folder, "google.txt");
Assert.AreEqual($@"{folder}\google.txt", path);
Assert.That(File.Exists(path));
File.Delete(path);
Directory.Delete(folder, true);
try {
var path = await "https://www.google.com".DownloadFileAsync(folder, "google.txt");
Assert.AreEqual($@"{folder}\google.txt", path);
Assert.That(File.Exists(path));
}
finally {
Directory.Delete(folder, true);
}
}

[Test]
public async Task can_download_file_with_default_name() {
var folder = "c:\\flurl-test-" + Guid.NewGuid(); // random so parallel tests don't trip over each other
try {
// no Content-Dispositon header, use last part of URL
var path = await "https://www.google.com".DownloadFileAsync(folder);
Assert.AreEqual($@"{folder}\www.google.com", path);
Assert.That(File.Exists(path));

// has Content-Disposition header but no filename in it, use last part of URL
path = await "https://httpbin.org/response-headers?Content-Disposition=attachment".DownloadFileAsync(folder);
Assert.AreEqual($@"{folder}\response-headers", path);
Assert.That(File.Exists(path));

// has header Content-Disposition: attachment; filename="myfile.txt"
path = await "https://httpbin.org/response-headers?Content-Disposition=attachment%3B%20filename%3D%22myfile.txt%22".DownloadFileAsync(folder);
Assert.AreEqual($@"{folder}\myfile.txt", path);
Assert.That(File.Exists(path));
}
finally {
Directory.Delete(folder, true);
}
}

[Test]
Expand Down
65 changes: 64 additions & 1 deletion Test/Flurl.Test/Http/SettingsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
Expand Down Expand Up @@ -187,7 +188,7 @@ public void can_configure_global_from_FlurlHttp_object() {

[Test]
public void can_configure_client_from_FlurlHttp_object() {
FlurlHttp.ConfigureClient("http://host1.com/foo", settings => settings.CookiesEnabled = true);
FlurlHttp.ConfigureClient("http://host1.com/foo", cli => cli.Settings.CookiesEnabled = true);
Assert.IsTrue(new FlurlRequest("https://host1.com/bar").Client.Settings.CookiesEnabled); // different URL but same host, so should use same client
Assert.IsFalse(new FlurlRequest("http://host2.com").Client.Settings.CookiesEnabled);
}
Expand All @@ -206,6 +207,31 @@ public class HttpTestSettingsTests : SettingsTestsBase

protected override FlurlHttpSettings GetSettings() => HttpTest.Current.Settings;
protected override IFlurlRequest GetRequest() => new FlurlRequest("http://api.com");

[Test] // github #246
public void test_settings_dont_override_request_settings_when_not_set_explicitily() {
var ser1 = new FakeSerializer();
var ser2 = new FakeSerializer();

using (var test = new HttpTest()) {
var cli = new FlurlClient();
cli.Settings.JsonSerializer = ser1;
Assert.AreSame(ser1, cli.Settings.JsonSerializer);

var req = new FlurlRequest { Client = cli };
Assert.AreSame(ser1, req.Settings.JsonSerializer);

req.Settings.JsonSerializer = ser2;
Assert.AreSame(ser2, req.Settings.JsonSerializer);
}
}

private class FakeSerializer : ISerializer
{
public string Serialize(object obj) => "foo";
public T Deserialize<T>(string s) => default(T);
public T Deserialize<T>(Stream stream) => default(T);
}
}

[TestFixture, Parallelizable]
Expand Down Expand Up @@ -265,6 +291,43 @@ public class RequestSettingsTests : SettingsTestsBase

protected override FlurlHttpSettings GetSettings() => _req.Value.Settings;
protected override IFlurlRequest GetRequest() => _req.Value;

[Test, NonParallelizable] // github #239
public void request_default_settings_change_when_client_changes() {
FlurlHttp.ConfigureClient("http://test.com", cli => cli.Settings.CookiesEnabled = true);
var req = new FlurlRequest("http://test.com");
var cli1 = req.Client;
Assert.IsTrue(req.Settings.CookiesEnabled, "pre-configured client should provide defaults to new request");

req.Url = "http://test.com/foo";
Assert.AreSame(cli1, req.Client, "new URL with same host should hold onto same client");
Assert.IsTrue(req.Settings.CookiesEnabled);

req.Url = "http://test2.com";
Assert.AreNotSame(cli1, req.Client, "new host should trigger new client");
Assert.IsFalse(req.Settings.CookiesEnabled);

FlurlHttp.ConfigureClient("http://test2.com", cli => cli.Settings.CookiesEnabled = true);
Assert.IsTrue(req.Settings.CookiesEnabled, "changing client settings should be reflected in request");

req.Settings = new FlurlHttpSettings();
Assert.IsTrue(req.Settings.CookiesEnabled, "entirely new settings object should still inherit current client settings");

req.Client = new FlurlClient();
Assert.IsFalse(req.Settings.CookiesEnabled, "entirely new client should provide new defaults");

req.Url = "http://test.com";
Assert.AreNotSame(cli1, req.Client, "client was explicitly set on request, so it shouldn't change even if the URL changes");
Assert.IsFalse(req.Settings.CookiesEnabled);
}

[Test]
public void request_gets_global_settings_when_no_client() {
var req = new FlurlRequest();
Assert.IsNull(req.Client);
Assert.IsNull(req.Url);
Assert.AreEqual(FlurlHttp.GlobalSettings.JsonSerializer, req.Settings.JsonSerializer);
}
}

public class SomeCustomHttpClientFactory : IHttpClientFactory
Expand Down
Loading

0 comments on commit 67734b1

Please sign in to comment.