Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FlurlRequest.Settings.JsonSerializer is ignored when using HttpTest #246

Closed
hmvs opened this issue Nov 24, 2017 · 6 comments
Closed

FlurlRequest.Settings.JsonSerializer is ignored when using HttpTest #246

hmvs opened this issue Nov 24, 2017 · 6 comments

Comments

@hmvs
Copy link

hmvs commented Nov 24, 2017

Seems to be Settings.JsonSerializer is ignored when using HttpTest

[Test]
public async Task serializer_for_request_should_be_used_in_testmode() {
	using (var httpTest = new HttpTest()) {
		var mockSerializer = new MockSerializer();
		var flurlRequest = new FlurlRequest("http://test.com/") {
			Settings = { JsonSerializer = mockSerializer }
		};
		//Assert.AreEqual(mockSerializer, flurlRequest.Settings.JsonSerializer);

		httpTest.RespondWith(@"{""Test"":""test""}");

		var testObj = await flurlRequest
			.PostJsonAsync(new {Test = "test"})
			.ReceiveJson<TestObj>();
		
		Assert.IsTrue(mockSerializer.DeserializeStreamUsed || mockSerializer.DeserializeUsed);

		httpTest.ShouldHaveCalled("http://test.com/");
	}
}

private class MockSerializer : ISerializer {
	public bool DeserializeUsed { get; set; }
	public bool DeserializeStreamUsed { get; set; }
	public string Serialize(object obj) {
		return @"{""Test"":""yoooooooooooooo""}";
	}
	public T Deserialize<T>(string s) {
		DeserializeUsed = true;
		return default(T);
	}
	public T Deserialize<T>(Stream stream) {
		DeserializeStreamUsed = true;
		return default(T);
	}
}

private class TestObj {
	public string Test { get; set; }
}

None of Deserialize methods was used.

@hmvs hmvs changed the title Settings.JsonSerializer is ignored when using HttpTest FlurlRequest.Settings.JsonSerializer is ignored when using HttpTest Nov 24, 2017
@tmenier
Copy link
Owner

tmenier commented Nov 27, 2017

Settings at the HttpTest level take precedence over everything, because it's far more likely that the subject under test would be messing with request-level settings and not the test itself, and the whole purpose of test-level settings is to override any settings inside the test subject, regardless of level. This should do what you want:

using (var httpTest = new HttpTest()) {
    httpTest.Settings.JsonSerializer = new MockSerializer();
    ...
}

Having said that, this still might be a bug. Since you didn't explicitly set a value at the test level, I think the request-level setting should have taken precedence over the global default. I will look into it, but in the mean time consider the above a work-around.

@hmvs
Copy link
Author

hmvs commented Nov 28, 2017

Yeah, thanks. Already used this workaround. Found it when I wanted to fix this issue. But haven't found any easy way how I can do it. As far as TestFlurlHttpSettings invokes ResetDefaults and actually adds ISerializer to dictionary, need some default value indicator.

@tmenier
Copy link
Owner

tmenier commented Nov 29, 2017

This is fixed, please test the prerelease: https://www.nuget.org/packages/Flurl.Http/2.1.0-pre

@hmvs
Copy link
Author

hmvs commented Nov 30, 2017

Works like a charm. Thank you.

@tmenier
Copy link
Owner

tmenier commented Dec 4, 2017

released

@tmenier tmenier closed this as completed Dec 4, 2017
@hmvs
Copy link
Author

hmvs commented Dec 4, 2017

Updated. Removed workaround. Works fine. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants