Skip to content

Commit

Permalink
Make parameter formatting more generic (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnii authored Jul 6, 2019
1 parent 99a5531 commit 4a86b7c
Show file tree
Hide file tree
Showing 30 changed files with 253 additions and 255 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ packages/

#resticle
targets
..\src\CommonAssemblyInfo.cs
..\src\CommonAssemblyInfo.cs

src/.idea/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 1.3.0

* Ability to change the way in which the query string is formatted

## 1.2.3

* Enabling post requests using form-data for a file
Expand Down
4 changes: 2 additions & 2 deletions src/SpeakEasy.Specifications/Bodies/FileUploadBodySpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class when_serializing
static IContent serializable;

Because of = () =>
serializable = body.Serialize(transmissionSettings, An<IArrayFormatter>());
serializable = body.Serialize(transmissionSettings, An<IQuerySerializer>());

It should_have_content_type_for_multipart_form_data = () =>
serializable.ShouldBeOfExactType<MultipartMimeContent>();
Expand Down Expand Up @@ -69,7 +69,7 @@ class when_serializing_a_multipart_form
body = new FileUploadBody(resource, new[] { An<IFile>() });

transmissionSettings = An<ITransmissionSettings>();
serializable = body.Serialize(transmissionSettings, An<IArrayFormatter>());
serializable = body.Serialize(transmissionSettings, An<IQuerySerializer>());
};

It should_have_content_type_for_multipart_form_data = () =>
Expand Down
120 changes: 120 additions & 0 deletions src/SpeakEasy.Specifications/DefaultQuerySerializerSpecs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using Machine.Specifications;
using SpeakEasy.Serializers;

namespace SpeakEasy.Specifications
{
[Subject(typeof(DefaultQuerySerializer))]
class DefaultQuerySerializerSpecs
{
static DefaultQuerySerializer serializer;

static IEnumerable<string> formatted;

Establish context = () =>
serializer = new DefaultQuerySerializer();

class in_general
{
It should_expand_array_values = () =>
serializer.ExpandArrayValues.ShouldBeTrue();
}

class when_converting_to_query_string
{
Because of = () =>
formatted = serializer.Serialize(new []
{
new Parameter("name", "value")
});

It should_format_as_query_string = () =>
formatted.ShouldContain("name=value");
}

class when_expanding_array_values
{
class when_converting_to_query_string_with_int_array_value_and_multiple_values_array_formatter
{
Establish context = () =>
serializer.ExpandArrayValues = true;

Because of = () =>
formatted = serializer.Serialize(new []
{
new Parameter("name", new[] { 3, 4, 5 })
});

It should_format_as_query_string = () =>
formatted.ShouldContain("name=3", "name=4", "name=5");
}
}

class when_not_expanding_array_values
{
Establish context = () =>
serializer.ExpandArrayValues = false;

class when_converting_to_query_string_with_string_array_value
{
Because of = () =>
formatted = serializer.Serialize(new []
{
new Parameter("name", new[] { "value1", "value2" })
});

It should_format_as_query_string = () =>
formatted.ShouldContain("name=value1,value2");
}

class when_converting_to_query_string_with_int_array_value
{
Because of = () =>
formatted = serializer.Serialize(new []
{
new Parameter("name", new[] { 3, 4, 5 })
});

It should_format_as_query_string = () =>
formatted.ShouldContain("name=3,4,5");
}
}

class when_converting_to_query_string_with_date_time
{
Because of = () =>
formatted = serializer.Serialize(new[]
{
new Parameter("name", new DateTime(2013, 10, 15, 14, 30, 44))
});

It should_format_as_query_string = () =>
formatted.ShouldContain("name=2013-10-15T14:30:44.0000000");
}

class when_converting_to_query_string_with_nullable_date_time
{
Because of = () =>
formatted = serializer.Serialize(new []
{
new Parameter("name", (DateTime?)new DateTime(2013, 10, 15, 14, 30, 44, DateTimeKind.Utc))
});

It should_format_as_query_string = () =>
formatted.ShouldContain("name=2013-10-15T14:30:44.0000000Z");
}

class when_converting_values_containing_slashes_and_ampersands
{
Because of = () =>
formatted = serializer.Serialize(new []
{
new Parameter("name", "value/this&that")
});

It should_properly_escape_special_characters = () =>
formatted.ShouldContain("name=value%2Fthis%26that");
}
}
}
7 changes: 3 additions & 4 deletions src/SpeakEasy.Specifications/HttpClientSettingsSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Threading;
using System.Threading.Tasks;
using Machine.Specifications;
using SpeakEasy.ArrayFormatters;
using SpeakEasy.Authenticators;
using SpeakEasy.Middleware;
using SpeakEasy.Serializers;
Expand All @@ -28,8 +27,8 @@ class in_general
It should_have_default_user_agent = () =>
settings.Middleware.Has<UserAgentMiddleware>().ShouldBeTrue();

It should_have_default_array_formatter = () =>
settings.ArrayFormatter.ShouldBeOfExactType<MultipleValuesArrayFormatter>();
It should_have_default_query_serializer = () =>
settings.QuerySerializer.ShouldBeOfExactType<DefaultQuerySerializer>();

It should_be_valid = () =>
settings.IsValid.ShouldBeTrue();
Expand All @@ -50,7 +49,7 @@ class default_settings_in_general
class without_array_formatter
{
Because of = () =>
settings.ArrayFormatter = null;
settings.QuerySerializer = null;

It should_not_be_valid = () =>
settings.IsValid.ShouldBeFalse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class RequestMiddlewareSpecs : WithFakes

Establish context = () =>
{
middleware = new RequestMiddleware(new SystemHttpClient(), The<ITransmissionSettings>(), The<IArrayFormatter>(), new CookieContainer());
middleware = new RequestMiddleware(new SystemHttpClient(), The<ITransmissionSettings>(), The<IQuerySerializer>(), new CookieContainer());
};

class when_building_web_request_with_get_request
Expand Down
21 changes: 0 additions & 21 deletions src/SpeakEasy.Specifications/MultipleValuesArrayFormatterSpecs.cs

This file was deleted.

88 changes: 1 addition & 87 deletions src/SpeakEasy.Specifications/ParameterSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using Machine.Specifications;
using SpeakEasy.ArrayFormatters;
using SpeakEasy.Serializers;

namespace SpeakEasy.Specifications
{
Expand All @@ -9,80 +9,6 @@ class ParameterSpecs
{
static Parameter parameter;

static string formatted;

class when_converting_to_query_string
{
Establish context = () =>
parameter = new Parameter("name", "value");

Because of = () =>
formatted = parameter.ToQueryString(new CommaSeparatedArrayFormatter());

It should_format_as_query_string = () =>
formatted.ShouldEqual("name=value");
}

class when_converting_to_query_string_with_string_array_value
{
Establish context = () =>
parameter = new Parameter("name", new[] { "value1", "value2" });

Because of = () =>
formatted = parameter.ToQueryString(new CommaSeparatedArrayFormatter());

It should_format_as_query_string = () =>
formatted.ShouldEqual("name=value1,value2");
}

class when_converting_to_query_string_with_int_array_value
{
Establish context = () =>
parameter = new Parameter("name", new[] { 3, 4, 5 });

Because of = () =>
formatted = parameter.ToQueryString(new CommaSeparatedArrayFormatter());

It should_format_as_query_string = () =>
formatted.ShouldEqual("name=3,4,5");
}

class when_converting_to_query_string_with_int_array_value_and_multiple_values_array_formatter
{
Establish context = () =>
parameter = new Parameter("name", new[] { 3, 4, 5 });

Because of = () =>
formatted = parameter.ToQueryString(new MultipleValuesArrayFormatter());

It should_format_as_query_string = () =>
formatted.ShouldEqual("name=3&name=4&name=5");
}

class when_converting_to_query_string_with_date_time
{
Establish context = () =>
parameter = new Parameter("name", new DateTime(2013, 10, 15, 14, 30, 44));

Because of = () =>
formatted = parameter.ToQueryString(new CommaSeparatedArrayFormatter());

It should_format_as_query_string = () =>
formatted.ShouldEqual("name=2013-10-15T14:30:44.0000000");
}

class when_converting_to_query_string_with_nullable_date_time
{
Establish context = () =>
parameter = new Parameter("name", (DateTime?)new DateTime(2013, 10, 15, 14, 30, 44, DateTimeKind.Utc));

Because of = () =>
formatted = parameter.ToQueryString(new CommaSeparatedArrayFormatter());

It should_format_as_query_string = () =>
formatted.ShouldEqual("name=2013-10-15T14:30:44.0000000Z");
}

class when_value_is_nullable
{
Establish context = () =>
Expand All @@ -91,17 +17,5 @@ class when_value_is_nullable
It should_not_have_value = () =>
parameter.HasValue.ShouldBeFalse();
}

class when_converting_values_containing_slashes_and_ampersands
{
Establish context = () =>
parameter = new Parameter("name", "value/this&that");

Because of = () =>
formatted = parameter.ToQueryString(new CommaSeparatedArrayFormatter());

It should_properly_escape_special_characters = () =>
formatted.ShouldEqual("name=value%2Fthis%26that");
}
}
}
2 changes: 1 addition & 1 deletion src/SpeakEasy.Specifications/RequestRunnerSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class RequestRunnerSpecs : WithSubject<RequestRunner>
Establish context = () =>
{
request = An<IHttpRequest>();
request.WhenToldTo(r => r.BuildRequestUrl(Param.IsAny<IArrayFormatter>())).Return("http://example.com");
request.WhenToldTo(r => r.BuildRequestUrl(Param.IsAny<IQuerySerializer>())).Return("http://example.com");
request.WhenToldTo(r => r.HttpMethod).Return(HttpMethod.Get);
};

Expand Down
4 changes: 2 additions & 2 deletions src/SpeakEasy.Specifications/Requests/GetRequestSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Machine.Specifications;
using SpeakEasy.ArrayFormatters;
using SpeakEasy.Requests;
using SpeakEasy.Serializers;

namespace SpeakEasy.Specifications.Requests
{
Expand Down Expand Up @@ -30,7 +30,7 @@ class when_building_web_request_with_parameters
};

Because of = () =>
url = request.BuildRequestUrl(new CommaSeparatedArrayFormatter());
url = request.BuildRequestUrl(new DefaultQuerySerializer());

It should_set_url = () =>
url.ShouldEqual("http://example.com/companies?filter=ftse&starred=True");
Expand Down
8 changes: 4 additions & 4 deletions src/SpeakEasy.Specifications/Requests/PostRequestSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
using System.Text;
using Machine.Fakes;
using Machine.Specifications;
using SpeakEasy.ArrayFormatters;
using SpeakEasy.Bodies;
using SpeakEasy.Requests;
using SpeakEasy.Serializers;

namespace SpeakEasy.Specifications.Requests
{
Expand Down Expand Up @@ -36,7 +36,7 @@ class when_building_request_url_with_object_body
};

It should_generate_query_params = () =>
request.BuildRequestUrl(new CommaSeparatedArrayFormatter()).ShouldEqual("http://example.com/companies?makemoney=allday");
request.BuildRequestUrl(new DefaultQuerySerializer()).ShouldEqual("http://example.com/companies?makemoney=allday");
}

class when_building_request_url_with_post_request_body
Expand All @@ -50,7 +50,7 @@ class when_building_request_url_with_post_request_body
};

It should_not_generate_query_params = () =>
request.BuildRequestUrl(new CommaSeparatedArrayFormatter()).ShouldEqual("http://example.com/companies");
request.BuildRequestUrl(new DefaultQuerySerializer()).ShouldEqual("http://example.com/companies");
}

class when_building_request_url_with_post_request_body_of_a_file
Expand Down Expand Up @@ -85,7 +85,7 @@ class when_building_request_url_with_post_request_body_of_a_file
Because of = () =>
{
var httpRequest = new HttpRequestMessage(HttpMethod.Post, "");
serializable = body.Serialize(An<ITransmissionSettings>(), An<IArrayFormatter>());
serializable = body.Serialize(An<ITransmissionSettings>(), An<IQuerySerializer>());
serializable.WriteTo(httpRequest).Wait();
content = (MultipartFormDataContent) httpRequest.Content;
};
Expand Down
Loading

0 comments on commit 4a86b7c

Please sign in to comment.