Skip to content
Open
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,19 @@ Utilize these tools to validate your JSON schema

## More Examples

### Add new response type parser
RestAssured can already parse JSON and XML response bodies, however you are able to overwrite
these with your own parsing functions or add new parsing functionality.
```C#
//Create a method to parse your input
public static dynamic CsvToJson(string input)
{
// Code to parse csv
}
//Then inside main method you add this parser
RestAssured.AddParser("csv",CsvToJson);
```

### Breaking up a call chain
```C#
//Create a new test suite
Expand Down
12 changes: 10 additions & 2 deletions src/RA/HttpActionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,15 @@ private ExecutionContext SetHttpAction(string url, HttpActionType actionType)

public void SetUrl(string url)
{
if (url.IsEmpty() && _setupContext.Host().IsEmpty())
if(!new System.Text.RegularExpressions.Regex(@":[0-9]{1,4}").IsMatch(url) && _setupContext.PortSpecified())
{
if (new System.Text.RegularExpressions.Regex(@"https?:\/\/").IsMatch(url))
url = url.Substring(0, url.IndexOf("/", 8)) + ":3005" + url.Substring(url.IndexOf("/", 8));
else
url = url.Substring(0, url.IndexOf("/")) + ":3005" + url.Substring(url.IndexOf("/"));
}

if (url.IsEmpty() && _setupContext.Host().IsEmpty())
throw new ArgumentException("url must be provided");

var uri = url.IsNotEmpty()
Expand All @@ -175,4 +183,4 @@ public void SetUrl(string url)
_url = uri.OriginalString;
}
}
}
}
77 changes: 53 additions & 24 deletions src/RA/ResponseContext.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -220,54 +220,83 @@ private void Initialize()
ParseLoad();
}

private void Parse()
/// <summary>
/// Holds response parser functions
/// </summary>
private static Dictionary<string, Func<string, dynamic>> parsers = new Dictionary<string, Func<string, dynamic>>()
{
var contentType = ContentType();

if (contentType.Contains("json"))
{
if (!string.IsNullOrEmpty(_content))
{ "json", delegate(string instr)
{
try
{
_parsedContent = JObject.Parse(_content);
return;
return JObject.Parse(instr);
}
catch
{
try
{
return JArray.Parse(instr);
}
catch(Exception e)
{
throw e;
}
}

}
},
{ "xml", delegate(string instr)
{
try
{
_parsedContent = JArray.Parse(_content);
return;
return XDocument.Parse(instr);
}
catch
catch(Exception e)
{
throw e;
}
}
else
{
return;
}
}
else if (contentType.Contains("xml"))
},
};

/// <summary>
/// Adds Parser to allow unsupporter response types to be parsed
/// </summary>
/// <param name="type">Content-Type to use provided function to parse</param>
/// <param name="func">Function to parse type</param>
internal static void AddParser(string type, Func<string, dynamic> func)
{
if (parsers.ContainsKey(type))
parsers[type] = func;
else
parsers.Add(type, func);
}

private void Parse()
{
if (string.IsNullOrEmpty(_content))
return;

var contentType = ContentType();

foreach (var type in parsers.Keys)
{
if (!string.IsNullOrEmpty(_content))
if (contentType.Contains(type))
{
try
{
_parsedContent = XDocument.Parse(_content);
_parsedContent = parsers[type](_content);
return;
}
catch
{
throw new Exception(string.Format("{0} parser failed to build json from data", type));
}
}
else
{
throw new Exception(string.Format("({0}) not supported", contentType));
}
}

if (!string.IsNullOrEmpty(_content))
throw new Exception(string.Format("({0}) not supported", contentType));
}

private void ParseLoad()
Expand Down Expand Up @@ -362,4 +391,4 @@ public ResponseContext WriteAssertions()
return this;
}
}
}
}
8 changes: 8 additions & 0 deletions src/RA/RestAssured.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,13 @@ public SetupContext Given()
{
return new SetupContext();
}

/// <summary>
/// Adds Parser to allow unsupporter response types to be parsed
/// </summary>
/// <param name="type">Content-Type to use provided function to parse</param>
/// <param name="func">Function to parse type</param>
internal static void AddParser(string type, System.Func<System.String, dynamic> func)
=> ResponseContext.AddParser(type, func);
}
}
2 changes: 1 addition & 1 deletion src/RA/SetupContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public string Host()
return PortSpecified() ? $"{_host}:{_port}":_host;
}

private bool PortSpecified()
internal bool PortSpecified()
{
return _port > 0 && _port != 88;
}
Expand Down