public void GetAllChannelsTest() { using (ShimsContext.Create()) { var responseStream = new FileStream("Fixtures/v1.channels.index.response", FileMode.Open); var responseShim = new ShimHttpWebResponse() { GetResponseStream = () => responseStream }; String actualMethod = ""; var requestShim = new ShimHttpWebRequest() { MethodSetString = (method) => { actualMethod = method; }, GetResponse = () => responseShim }; String actualURL = ""; ShimWebRequest.CreateString = (url) => { actualURL = url; return requestShim; }; Channel ch = new Channel(); JArray json = ch.getAllBroadcastsChannels(); StringAssert.AreEqualIgnoringCase("GET", actualMethod); StringAssert.AreEqualIgnoringCase("http://panoply-staging.herokuapp.com/api/channels.json", actualURL); Assert.AreEqual(2, json.Count); } }
public void SkytapApi_SendSkytapHttpRequest_Success() { using (ShimsContext.Create()) { // Arrange var webHeaders = new ShimWebHeaderCollection(); webHeaders.ToString = () => "TestString"; var response = new ShimHttpWebResponse(); response.StatusCodeGet = () => HttpStatusCode.OK; response.HeadersGet = () => webHeaders; response.GetResponseStream = () => new MemoryStream(System.Text.Encoding.UTF8.GetBytes("BodyString")); var request = new ShimHttpWebRequest(); request.GetResponse = () => response; // This was experimental code that is left here for reference purposes. It shows how to // simulate an exception being thrown in a shimmed object's method. // //var webException = new ShimHttpException(); //ShimHttpWebRequest.AllInstances.GetResponse = url => { throw (HttpException)webException; }; // Act var responseString = SkytapApi.SendSkytapHttpRequest(request); // Assert Assert.IsFalse(string.IsNullOrEmpty(responseString)); } }
public void SkytapApi_SendSkytapHttpRequest_BadHttpStatuses() { var badHttpStatuses = new[] {HttpStatusCode.NotFound, HttpStatusCode.ServiceUnavailable}; using (ShimsContext.Create()) { foreach (var httpStatus in badHttpStatuses) { var response = new ShimHttpWebResponse(); var status = httpStatus; // required as accessing foreach variable may have different behavior with different compilers response.StatusCodeGet = () => status; var request = new ShimHttpWebRequest(); request.GetResponse = () => response; try { // The following call should trigger an exception so the next line should never be hit SkytapApi.SendSkytapHttpRequest(request); Assert.Fail("Exception was expected; fail the test"); } catch (Exception e) { StringAssert.Contains(e.Message, ((int)httpStatus).ToString(CultureInfo.InvariantCulture)); } } } }
private GeoCodeAddressResponseModel.WebServiceGeocodeQueryResultSet SendData(HttpStatusCode statusCode, string dataToSend, GeoCodeAddressModel.GeocodeAddressNonParsed addressData) { using (ShimsContext.Create()) { var requestShim = new ShimHttpWebRequest(); var responseShim = new ShimHttpWebResponse(); ShimWebRequest.CreateHttpString = (url) => requestShim.Instance; requestShim.HeadersGet = () => new WebHeaderCollection(); ExpectSerialize(dataToSend); GeoCodeAddressResponseModel.WebServiceGeocodeQueryResultSet serviceResponse = GeoCodeAddressModelResponseSample.TypedSample; ExpectDeSerialize(serviceResponse); using (Stream requestStream = BuildStream(), responseStream = BuildStream(dataToSend)) { requestShim.GetRequestStream = () => requestStream; requestShim.GetResponse = () => responseShim.Instance; responseShim.StatusCodeGet = () => statusCode; responseShim.GetResponseStream = () => responseStream; GeoCodeAddressResponseModel.WebServiceGeocodeQueryResultSet result; Assert.IsTrue(Client.SendData(addressData, out result)); Assert.IsNotNull(result); return result; } } }