Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions RestSharp.Tests/RestRequestTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Xunit;
using System.Globalization;
using Xunit.Extensions;

namespace RestSharp.Tests {
public class RestRequestTests {
Expand All @@ -17,5 +19,54 @@ public void Can_Add_Object_With_IntegerArray_property() {
var request = new RestRequest();
request.AddObject(new { Items = new int[] { 2, 3, 4 } });
}

[Fact]
public void Cannot_Set_Empty_Host_Header()
{
var request = new RestRequest();

var exception = Assert.Throws<ArgumentException>(() => request.AddHeader("Host", string.Empty));
Assert.Equal("value", exception.ParamName);
}

[Theory]
[InlineData("http://localhost")]
[InlineData("hostname 1234")]
[InlineData("-leading.hyphen.not.allowed")]
[InlineData("bad:port")]
[InlineData(" no.leading.white-space")]
[InlineData("no.trailing.white-space ")]
[InlineData(".leading.dot.not.allowed")]
[InlineData("double.dots..not.allowed")]
[InlineData(".")]
[InlineData(".:2345")]
[InlineData(":5678")]
public void Cannot_Set_Invalid_Host_Header(string value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the empty string case?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, multiple colon case? "foo:bar:baz"

{
var request = new RestRequest();

var exception = Assert.Throws<ArgumentException>(() => request.AddHeader("Host", value));
Assert.Equal("value", exception.ParamName);
}

[Theory]
[InlineData("localhost")]
[InlineData("localhost:1234")]
[InlineData("host.local")]
[InlineData("anotherhost.local:2345")]
[InlineData("www.w3.org")]
[InlineData("www.w3.org:3456")]
[InlineData("8.8.8.8")]
[InlineData("a.1.b.2")]
[InlineData("10.20.30.40:1234")]
[InlineData("0host")]
[InlineData("hypenated-hostname")]
[InlineData("multi--hyphens")]
public void Can_Set_Valid_Host_Header(string value)
{
var request = new RestRequest();

Assert.DoesNotThrow(() => request.AddHeader("Host", value));
}
}
}
7 changes: 7 additions & 0 deletions RestSharp/RestRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using RestSharp.Extensions;
using RestSharp.Serializers;

Expand Down Expand Up @@ -356,6 +357,12 @@ public IRestRequest AddParameter (string name, object value, ParameterType type)
/// <returns></returns>
public IRestRequest AddHeader (string name, string value)
{
const string portSplit = @":\d+";
Func<string, bool> invalidHost = host => Uri.CheckHostName(Regex.Split(host, portSplit)[0]) == UriHostNameType.Unknown;
if (name == "Host" && invalidHost(value))
{
throw new ArgumentException("The specified value is not a valid Host header string.", "value");
}
return AddParameter(name, value, ParameterType.HttpHeader);
}

Expand Down