public virtual string GetWebResponse(HttpWebRequest webRequest, string content = null) { if (content != null) { using (var writer = new StreamWriter(webRequest.GetRequestStream())) { writer.Write(content); writer.Close(); } } string result; object userData = null; if (BeforeRequest != null) { var arg = new BeforeRequestEvent(webRequest, content); BeforeRequest(arg); userData = arg.UserData; } var webResponse = (HttpWebResponse)webRequest.GetResponse(); using (var stream = webResponse.GetResponseStream()) { if (stream == null) return null; var sr = new StreamReader(stream); result = sr.ReadToEnd(); sr.Close(); } if (AfterRequest != null) { var arg = new AfterRequestEvent(webRequest, content, webResponse, result, userData); AfterRequest(arg); } return result; }
/// <summary> /// Process the request and returns the response to be asserted. /// </summary> /// <param name="autoAssertWhenAddingRules">When <code>true</code> it will validate the rule as they are added. Default is <code>false</code></param> /// <returns></returns> public RestResponse Response(bool autoAssertWhenAddingRules = false) { var clientBuilder = Configuration.ClientFactory; if (clientBuilder == null) { throw new InvalidOperationException(ErrorMessages.NoClientFactory); } var responseDeserialiser = ResponseDeserialiser ?? Configuration.ResponseDeserialiser; if (responseDeserialiser == null) { throw new InvalidOperationException(ErrorMessages.NoResponseDeserialiser); } using (var client = clientBuilder.Create()) { var context = new AssertionContext { Request = BuildRequest(clientBuilder), Client = client, ResponseDeserialiser = responseDeserialiser }; if (BeforeRequestEvent != null || Configuration.BeforeRequestEvent != null) { var requestContext = new RequestContext(context); Configuration.BeforeRequestEvent?.Invoke(requestContext); BeforeRequestEvent?.Invoke(requestContext); } context.Response = client.ExecuteRequest(context.Request); Configuration.AfterRequestEvent?.Invoke(context); AfterRequestEvent?.Invoke(context); var response = new RestResponse(this, context, autoAssertWhenAddingRules); return(response); } }