Skip to content

Instantly share code, notes, and snippets.

@judwhite
Created November 5, 2016 21:37
Show Gist options
  • Save judwhite/5f25b038cb6b4828f887a019946ea645 to your computer and use it in GitHub Desktop.
Save judwhite/5f25b038cb6b4828f887a019946ea645 to your computer and use it in GitHub Desktop.
[TestMethod]
public void TestHttpHeaderOperations()
{
// Create functions which return an Action<HttpHeader> function so we can build our "act" sets.
//
// Example:
// new[] {
// add("Content-Type", "application/json")
// delete("Content-Type")
// }
var add = new Func<string, string, Action<HttpHeaders>>((field, value) =>
headers => headers.Add(field, value)
);
var set = new Func<string, string, Action<HttpHeaders>>((field, value) =>
headers => headers.Set(field, value)
);
var setIndexer = new Func<string, string, Action<HttpHeaders>>((field, value) =>
headers => headers[field] = value
);
var delete = new Func<string, Action<HttpHeaders>>(field =>
headers => headers.Delete(field)
);
var cases = new[] {
// Case 1: Add single value
new {
Operations = new[] {
add("Accept", "application/json")
},
Expected = new[] {
new { Field = "Accept", Value = "application/json" }
}
},
// Case 2: Case-insensitive "Get"
new {
Operations = new[] {
add("Content-Length", "100")
},
Expected = new[] {
new { Field = "content-length", Value = "100" }
}
},
// Case 3: Add multiple header values
new {
Operations = new[] {
add("Accept", "application/json"),
add("Content-Length", "100"),
},
Expected = new[]
{
new { Field = "Accept", Value = "application/json" },
new { Field = "Content-Length", Value = "100" },
}
},
// Case 4: Add repeated fields; Get returns first added
new {
Operations = new[] {
add("accept", "text/html"),
add("Content-Length", "10" ),
add("Accept", "application/json"),
add("content-length", "100"),
},
Expected = new[] {
new { Field = "Accept", Value = "text/html" },
new { Field = "Content-Length", Value = "10" },
}
},
// Case 5: Non-existant entry returns string.Empty
new {
Operations = new[] {
add("Accept", "application/json"),
},
Expected = new[] {
new { Field = "Content-Length", Value = string.Empty },
}
},
// Case 6: "Add" null results in "Get" string.Empty
new {
Operations = new[] {
add("Accept", null)
},
Expected = new[] {
new { Field = "Accept", Value = string.Empty }
}
},
// Case 7: "Add" null doesn't affect previous "Add"
new {
Operations = new[] {
add("Accept", "text/xml"),
add("Accept", null),
},
Expected = new[] {
new { Field = "Accept", Value = "text/xml" }
}
},
// Case 8: Simple set
new {
Operations = new[] {
set("Accept", "text/plain")
},
Expected = new[] {
new { Field = "Accept", Value = "text/plain" }
}
},
// Case 9: Set should overwrite previous value(s) for given field
new {
Operations = new[] {
set("Accept", "text/plain"),
set("Accept", "application/json"),
},
Expected = new[] {
new { Field = "Accept", Value = "application/json" }
}
},
// Case 10: Set should overwrite, Add after Set shouldn't affect Get
new {
Operations = new[] {
set("Accept", "text/plain"),
set("Accept", "application/json"),
add("Accept", "text/html"),
},
Expected = new[] {
new { Field = "Accept", Value = "application/json" }
}
},
// Case 11: Delete a Set field
new {
Operations = new[] {
set("Accept", "text/plain"),
delete("Accept"),
},
Expected = new[] {
new { Field = "Accept", Value = string.Empty }
}
},
// Case 12: Set a previously Set field to null
new {
Operations = new[] {
set("Accept", "text/plain"),
set("Accept", null),
},
Expected = new[] {
new { Field = "Accept", Value = string.Empty }
}
},
// Case 13: Set using indexer overwrites add
new {
Operations = new[] {
add("Accept", "text/plain"),
setIndexer("Accept", "application/json"),
},
Expected = new[] {
new { Field = "Accept", Value = "application/json" }
}
},
// Case 14: Set using indexer deletes when value is null
new {
Operations = new[] {
add("Accept", "text/plain"),
setIndexer("Accept", null),
},
Expected = new[] {
new { Field = "Accept", Value = string.Empty }
}
},
};
for (int i = 0; i < cases.Length; i++)
{
var c = cases[i];
// Arrange
var headers = new HttpHeaders();
// Act
foreach (var op in c.Operations)
{
op(headers);
}
// Assert
foreach (var expected in c.Expected)
{
var getActual = headers.Get(expected.Field);
Assert.AreEqual(expected.Value, getActual,
string.Format("Case: {0} Get(\"{1}\")", i + 1, expected.Field));
var indexerActual = headers[expected.Field];
Assert.AreEqual(expected.Value, indexerActual,
string.Format("Case: {0} headers[\"{1}\"]", i + 1, expected.Field));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment