public void Source_Discovery_Http_Error_LinkbackReceiveException_Saved() { // Setup var request = new Mock <HttpRequestBase>(MockBehavior.Strict); string request_content = String.Format("<?xml version=\"1.0\"?><methodCall><methodName>pingback.ping</methodName><params><param><value><string>{0}</string></value></param><param><value><string>{1}</string></value></param></params></methodCall>", "http://source/1", "http://target/1"); request.SetupGet(x => x.InputStream).Returns(new MemoryStream(new UTF8Encoding().GetBytes(request_content))); var webRequest = new Mock <IHttpWebRequestImplementation>(MockBehavior.Strict); var webResponse = new Mock <IHttpWebResponseImplementation>(MockBehavior.Strict); webRequest.Setup(x => x.Create(new Uri("http://source/1", UriKind.Absolute))).Returns(webRequest.Object); webRequest.Setup(x => x.GetResponse()).Returns(webResponse.Object); webResponse.Setup(x => x.Close()); webResponse.SetupGet(x => x.IsHttpStatusCode2XX).Returns(false); // Test var pingback = new Pingback(webRequest.Object); var result = pingback.Receive(request.Object, null); // Verify request.VerifyAll(); webRequest.VerifyAll(); webResponse.VerifyAll(); Assert.False(result.Valid); Assert.IsType <LinkbackReceiveException>(result.ReceiveException); Assert.Equal("Http error while discovering Pingback source at http://source/1", result.ReceiveException.Message); }
public void ProtocolViolationException_Saved() { // Setup var webRequest1 = new Mock <IHttpWebRequestImplementation>(MockBehavior.Strict); var webResponse1 = new Mock <IHttpWebResponseImplementation>(MockBehavior.Strict); webRequest1.Setup(x => x.Create(new Uri("http://target/1", UriKind.Absolute))).Returns(webRequest1.Object); webRequest1.Setup(x => x.GetResponse()).Throws(new System.Net.ProtocolViolationException("ProtocolViolationException")); // Test var pingback = new Pingback(webRequest1.Object); var url = new Uri("http://target/1"); var parameters = new LinkbackSendParameters { SourceUrl = new Uri("http://source/1"), TargetUrl = new Uri("http://target/1") }; var result = pingback.Send(url, parameters); // Verify webRequest1.VerifyAll(); webResponse1.VerifyAll(); Assert.False(result.Success); Assert.Equal(0, result.Code); Assert.True(String.IsNullOrEmpty(result.Message)); Assert.IsType <System.Net.ProtocolViolationException>(result.SendException); }
public void Request_Params_Do_Not_Contain_SourceURL() { // Setup var request = new Mock <HttpRequestBase>(MockBehavior.Strict); string request_content = String.Format("<?xml version=\"1.0\"?><methodCall><methodName>pingback.ping</methodName><params><param><value><string>{0}</string></value></param></params></methodCall>", "http://target/1"); request.SetupGet(x => x.InputStream).Returns(new MemoryStream(new UTF8Encoding().GetBytes(request_content))); var webRequest = new Mock <IHttpWebRequestImplementation>(MockBehavior.Strict); // Test var pingback = new Pingback(webRequest.Object); var result = pingback.Receive(request.Object, null); // Verify request.VerifyAll(); webRequest.VerifyAll(); Assert.False(result.Valid); Assert.Null(result.SourceUri); Assert.Null(result.TargetUri); }
public void Request_Params_Invalid_TargetURI() { // Setup var request = new Mock <HttpRequestBase>(MockBehavior.Strict); string request_content = String.Format("<?xml version=\"1.0\"?><methodCall><methodName>pingback.ping</methodName><params><param><value><string>{0}</string></value></param><param><value><string>{1}</string></value></param></params></methodCall>", "http://source/1", "~this~is~not~an~url*xxx://"); request.SetupGet(x => x.InputStream).Returns(new MemoryStream(new UTF8Encoding().GetBytes(request_content))); var webRequest = new Mock <IHttpWebRequestImplementation>(MockBehavior.Strict); // Test var pingback = new Pingback(webRequest.Object); var result = pingback.Receive(request.Object, null); // Verify request.VerifyAll(); webRequest.VerifyAll(); Assert.False(result.Valid); Assert.Equal("http://source/1", result.SourceUri.ToString().ToLowerInvariant()); Assert.Null(result.TargetUri); }
public void Http_Error_LinkbackSendExceptions_Saved() { // Setup var webRequest1 = new Mock <IHttpWebRequestImplementation>(MockBehavior.Strict); var webResponse1 = new Mock <IHttpWebResponseImplementation>(MockBehavior.Strict); webRequest1.Setup(x => x.Create(new Uri("http://target/1", UriKind.Absolute))).Returns(webRequest1.Object); webRequest1.Setup(x => x.GetResponse()).Returns(webResponse1.Object); webResponse1.Setup(x => x.Close()); webResponse1.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true); webResponse1.SetupGet(x => x.Headers).Returns( new System.Net.WebHeaderCollection { { "X-Pingback", "http://target/pingback" } }); var webRequest2 = new Mock <IHttpWebRequestImplementation>(MockBehavior.Strict); var webResponse2 = new Mock <IHttpWebResponseImplementation>(MockBehavior.Strict); webRequest1.Setup(x => x.Create(new Uri("http://target/pingback", UriKind.Absolute))).Returns(webRequest2.Object); webRequest2.SetupSet(x => x.Method = "POST"); webRequest2.SetupSet(x => x.ContentType = "text/xml"); webRequest2.SetupSet(x => x.ContentLength = 225); var requestStream = new MemoryStream(); webRequest2.Setup(x => x.GetRequestStream()).Returns(requestStream); webRequest2.Setup(x => x.GetResponse()).Returns(webResponse2.Object); webResponse2.Setup(x => x.Close()); webResponse2.SetupGet(x => x.IsHttpStatusCode2XX).Returns(false); // Test var pingback = new Pingback(webRequest1.Object); var url = new Uri("http://target/1"); var parameters = new LinkbackSendParameters { SourceUrl = new Uri("http://source/1"), TargetUrl = new Uri("http://target/1") }; var result = pingback.Send(url, parameters); // Verify webRequest1.VerifyAll(); webResponse1.VerifyAll(); webRequest2.VerifyAll(); webResponse2.VerifyAll(); Assert.False(result.Success); Assert.Equal(0, result.Code); Assert.True(String.IsNullOrEmpty(result.Message)); Assert.IsType <LinkbackSendException>(result.SendException); Assert.Equal("Http error while sending Pingback for http://target/pingback", result.SendException.Message); }
public void Discovery_Header() { // Setup var webRequest1 = new Mock <IHttpWebRequestImplementation>(MockBehavior.Strict); var webResponse1 = new Mock <IHttpWebResponseImplementation>(MockBehavior.Strict); webRequest1.Setup(x => x.Create(new Uri("http://target/1", UriKind.Absolute))).Returns(webRequest1.Object); webRequest1.Setup(x => x.GetResponse()).Returns(webResponse1.Object); webResponse1.Setup(x => x.Close()); webResponse1.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true); webResponse1.SetupGet(x => x.Headers).Returns( new System.Net.WebHeaderCollection { { "X-Pingback", "http://target/pingback" } }); var webRequest2 = new Mock <IHttpWebRequestImplementation>(MockBehavior.Strict); var webResponse2 = new Mock <IHttpWebResponseImplementation>(MockBehavior.Strict); webRequest1.Setup(x => x.Create(new Uri("http://target/pingback", UriKind.Absolute))).Returns(webRequest2.Object); webRequest2.SetupSet(x => x.Method = "POST"); webRequest2.SetupSet(x => x.ContentType = "text/xml"); webRequest2.SetupSet(x => x.ContentLength = 225); var requestStream = new MemoryStream(); webRequest2.Setup(x => x.GetRequestStream()).Returns(requestStream); webRequest2.Setup(x => x.GetResponse()).Returns(webResponse2.Object); webResponse2.Setup(x => x.Close()); webResponse2.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true); webResponse2.SetupGet(x => x.ContentEncoding).Returns(String.Empty); webResponse2.Setup(x => x.GetResponseStream()).Returns(new MemoryStream(new UTF8Encoding().GetBytes("<?xml version=\"1.0\"?><methodResponse><params><param><value><string>ok</string></value></param></params></methodResponse>"))); // Test var pingback = new Pingback(webRequest1.Object); var url = new Uri("http://target/1"); var parameters = new LinkbackSendParameters { SourceUrl = new Uri("http://source/1"), TargetUrl = new Uri("http://target/1") }; var result = pingback.Send(url, parameters); // Verify webRequest1.VerifyAll(); webResponse1.VerifyAll(); webRequest2.VerifyAll(); webResponse2.VerifyAll(); Assert.True(result.Success); Assert.Equal(0, result.Code); Assert.True(String.IsNullOrEmpty(result.Message)); }
public void Verify(Pingback pingback) { var akismet = Connect(); var akismetComment = new AkismetComment(); akismetComment.Blog = "http://www.funnelweblog.com"; akismetComment.CommentAuthorUrl = pingback.TargetUri; akismetComment.CommentContent = pingback.TargetUri; akismetComment.CommentType = "pingback"; pingback.IsSpam = akismet.CommentCheck(akismetComment); }
public void Receive() { // Setup var request = new Mock <HttpRequestBase>(MockBehavior.Strict); string request_content = String.Format("<?xml version=\"1.0\"?><methodCall><methodName>pingback.ping</methodName><params><param><value><string>{0}</string></value></param><param><value><string>{1}</string></value></param></params></methodCall>", "http://source/1", "http://target/1"); request.SetupGet(x => x.InputStream).Returns(new MemoryStream(new UTF8Encoding().GetBytes(request_content))); var webRequest = new Mock <IHttpWebRequestImplementation>(MockBehavior.Strict); var webResponse = new Mock <IHttpWebResponseImplementation>(MockBehavior.Strict); webRequest.Setup(x => x.Create(new Uri("http://source/1", UriKind.Absolute))).Returns(webRequest.Object); webRequest.Setup(x => x.GetResponse()).Returns(webResponse.Object); webResponse.Setup(x => x.Close()); webResponse.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true); string source = @" ... <head> <title>Source Title</title> </head> <div> <p> This is a text <a href=""http://tarGet/1"">post1</a> with link </p> </div> ... "; webResponse.Setup(x => x.GetResponseStream()).Returns(new MemoryStream(new UTF8Encoding().GetBytes(source))); // Test var pingback = new Pingback(webRequest.Object); var result = pingback.Receive(request.Object, null); // Verify request.VerifyAll(); webRequest.VerifyAll(); webResponse.VerifyAll(); Assert.True(result.Valid); Assert.Equal("http://source/1", result.SourceUri.ToString().ToLowerInvariant()); Assert.Equal("http://target/1", result.TargetUri.ToString().ToLowerInvariant()); Assert.Equal("Source Title", result.Title); Assert.Equal("Source Title", result.BlogName); Assert.Equal("This is a text post1 with link", result.Excerpt); }
public JsonResult Add(CreateResourceModel model, CurrentUserInformation currentUser) { Term word = Session.Load <Term>(model.TermId); Resource resource = new Resource(); resource.Id = Guid.NewGuid(); resource.CreatedOn = DateTime.UtcNow; resource.Title = model.Title; resource.Description = model.Description; resource.Url = model.Url; resource.EmbedCode = model.EmbedCode; resource.ThumbnailUrl = model.Thumbnail; resource.Type = model.Type; resource.ViaSource = model.ViaSource; if (word.Resources == null) { word.Resources = new List <Resource>(); } word.Resources.Add(resource); if (currentUser.IsAuthenticated) { resource.SharedByUserId = currentUser.Id; var user = Session.Load <User>(currentUser.Id); user.ReputationPoints += ActionPoints.AddInformation; if (user.ActionFeed == null) { user.ActionFeed = new List <Web.Models.Action>(); } user.ActionFeed.Add(new Web.Models.Action() { Time = DateTime.UtcNow, Type = ActionType.AddInformation, ResourceIdContext = resource.Id, TermIdContext = model.TermId }); Session.Store(user); } Session.Store(word); Session.SaveChanges(); // Pingback for blogger UrlHelper helper = new UrlHelper(this.ControllerContext.RequestContext); var source = helper.Action("Detail", "Term", new { Slug = word.Slug }, "http"); Pingback.Send(new Uri(source), new Uri(resource.Url)); return(Json(word)); }
public void Discovery_Both_Header_And_Link_Not_Found_Then_LinkbackSendException_Saved() { // Setup var webRequest1 = new Mock <IHttpWebRequestImplementation>(MockBehavior.Strict); var webResponse1 = new Mock <IHttpWebResponseImplementation>(MockBehavior.Strict); webRequest1.Setup(x => x.Create(new Uri("http://target/1", UriKind.Absolute))).Returns(webRequest1.Object); webRequest1.Setup(x => x.GetResponse()).Returns(webResponse1.Object); webResponse1.Setup(x => x.Close()); webResponse1.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true); webResponse1.SetupGet(x => x.ContentEncoding).Returns(String.Empty); webResponse1.SetupGet(x => x.Headers).Returns(new System.Net.WebHeaderCollection { }); string response_content = "..."; webResponse1.Setup(x => x.GetResponseStream()).Returns(new MemoryStream(new UTF8Encoding().GetBytes(response_content))); // Test var pingback = new Pingback(webRequest1.Object); var url = new Uri("http://target/1"); var parameters = new LinkbackSendParameters { SourceUrl = new Uri("http://source/1"), TargetUrl = new Uri("http://target/1") }; var result = pingback.Send(url, parameters); // Verify webRequest1.VerifyAll(); webResponse1.VerifyAll(); Assert.False(result.Success); Assert.Equal(0, result.Code); Assert.True(String.IsNullOrEmpty(result.Message)); Assert.IsType <LinkbackSendException>(result.SendException); Assert.Equal("Pingback url discovering failed for http://target/1", result.SendException.Message); }
public void Discovery_Http_Error_Then_LinkbackSendException_Saved() { // Setup var webRequest1 = new Mock <IHttpWebRequestImplementation>(MockBehavior.Strict); var webResponse1 = new Mock <IHttpWebResponseImplementation>(MockBehavior.Strict); webRequest1.Setup(x => x.Create(new Uri("http://target/1", UriKind.Absolute))).Returns(webRequest1.Object); webRequest1.Setup(x => x.GetResponse()).Returns(webResponse1.Object); webResponse1.Setup(x => x.Close()); webResponse1.SetupGet(x => x.IsHttpStatusCode2XX).Returns(false); // Test var pingback = new Pingback(webRequest1.Object); var url = new Uri("http://target/1"); var parameters = new LinkbackSendParameters { SourceUrl = new Uri("http://source/1"), TargetUrl = new Uri("http://target/1") }; var result = pingback.Send(url, parameters); // Verify webRequest1.VerifyAll(); webResponse1.VerifyAll(); Assert.False(result.Success); Assert.Equal(0, result.Code); Assert.True(String.IsNullOrEmpty(result.Message)); Assert.IsType <LinkbackSendException>(result.SendException); Assert.Equal("Http error while discovering Pingback url for http://target/1", result.SendException.Message); }
public void Discovery_Source_Has_No_Link() { // Setup var request = new Mock <HttpRequestBase>(MockBehavior.Strict); string request_content = String.Format("<?xml version=\"1.0\"?><methodCall><methodName>pingback.ping</methodName><params><param><value><string>{0}</string></value></param><param><value><string>{1}</string></value></param></params></methodCall>", "http://source/1", "http://target/1"); request.SetupGet(x => x.InputStream).Returns(new MemoryStream(new UTF8Encoding().GetBytes(request_content))); var webRequest = new Mock <IHttpWebRequestImplementation>(MockBehavior.Strict); var webResponse = new Mock <IHttpWebResponseImplementation>(MockBehavior.Strict); webRequest.Setup(x => x.Create(new Uri("http://source/1", UriKind.Absolute))).Returns(webRequest.Object); webRequest.Setup(x => x.GetResponse()).Returns(webResponse.Object); webResponse.Setup(x => x.Close()); webResponse.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true); string source = "... http://ta-get/1 ..."; webResponse.Setup(x => x.GetResponseStream()).Returns(new MemoryStream(new UTF8Encoding().GetBytes(source))); // Test var pingback = new Pingback(webRequest.Object); var result = pingback.Receive(request.Object, null); // Verify request.VerifyAll(); webRequest.VerifyAll(); webResponse.VerifyAll(); Assert.False(result.Valid); Assert.Equal("http://source/1", result.SourceUri.ToString().ToLowerInvariant()); Assert.Equal("http://target/1", result.TargetUri.ToString().ToLowerInvariant()); }
public void Unknown_Exceptions_Are_Not_Handled() { // Setup var webRequest1 = new Mock <IHttpWebRequestImplementation>(MockBehavior.Strict); var webResponse1 = new Mock <IHttpWebResponseImplementation>(MockBehavior.Strict); webRequest1.Setup(x => x.Create(new Uri("http://target/1", UriKind.Absolute))).Returns(webRequest1.Object); webRequest1.Setup(x => x.GetResponse()).Throws(new InvalidOperationException()); // Test var pingback = new Pingback(webRequest1.Object); var url = new Uri("http://target/1"); var parameters = new LinkbackSendParameters { SourceUrl = new Uri("http://source/1"), TargetUrl = new Uri("http://target/1") }; InvalidOperationException exception = null; try { pingback.Send(url, parameters); } catch (InvalidOperationException ex) { exception = ex; } // Verify Assert.NotNull(exception); }
public void Delete(Pingback pingback) { session.Delete(pingback); }
public void Send_Response_Is_GZipped() { // Setup var webRequest1 = new Mock <IHttpWebRequestImplementation>(MockBehavior.Strict); var webResponse1 = new Mock <IHttpWebResponseImplementation>(MockBehavior.Strict); webRequest1.Setup(x => x.Create(new Uri("http://target/1", UriKind.Absolute))).Returns(webRequest1.Object); webRequest1.Setup(x => x.GetResponse()).Returns(webResponse1.Object); webResponse1.Setup(x => x.Close()); webResponse1.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true); webResponse1.SetupGet(x => x.Headers).Returns(new System.Net.WebHeaderCollection { }); webResponse1.SetupGet(x => x.ContentEncoding).Returns("gzip"); string response_content = @" ... <p> <link rel=""pingback"" href=""http://target/pingback""> </p> ..."; var response_content_bytes = new UTF8Encoding().GetBytes(response_content); var memoryStream = new MemoryStream(); var gzippedStream = new GZipStream(memoryStream, CompressionMode.Compress, true); gzippedStream.Write(response_content_bytes, 0, response_content_bytes.Length); gzippedStream.Close(); memoryStream.Position = 0; webResponse1.Setup(x => x.GetResponseStream()).Returns(memoryStream); var webRequest2 = new Mock <IHttpWebRequestImplementation>(MockBehavior.Strict); var webResponse2 = new Mock <IHttpWebResponseImplementation>(MockBehavior.Strict); webRequest1.Setup(x => x.Create(new Uri("http://target/pingback", UriKind.Absolute))).Returns(webRequest2.Object); webRequest2.SetupSet(x => x.Method = "POST"); webRequest2.SetupSet(x => x.ContentType = "text/xml"); webRequest2.SetupSet(x => x.ContentLength = 225); var requestStream = new MemoryStream(); webRequest2.Setup(x => x.GetRequestStream()).Returns(requestStream); webRequest2.Setup(x => x.GetResponse()).Returns(webResponse2.Object); webResponse2.Setup(x => x.Close()); webResponse2.SetupGet(x => x.IsHttpStatusCode2XX).Returns(true); webResponse2.SetupGet(x => x.ContentEncoding).Returns("gzip"); string responseContent = "<?xml version=\"1.0\"?><methodResponse><params><param><value><string>ok</string></value></param></params></methodResponse>"; var responseContentBytes = new UTF8Encoding().GetBytes(responseContent); var responseMemoryStream = new MemoryStream(); var gzippedResponseStream = new GZipStream(responseMemoryStream, CompressionMode.Compress, true); gzippedResponseStream.Write(responseContentBytes, 0, responseContentBytes.Length); gzippedResponseStream.Close(); responseMemoryStream.Position = 0; webResponse2.Setup(x => x.GetResponseStream()).Returns(responseMemoryStream); // Test var pingback = new Pingback(webRequest1.Object); var url = new Uri("http://target/1"); var parameters = new LinkbackSendParameters { SourceUrl = new Uri("http://source/1"), TargetUrl = new Uri("http://target/1") }; var result = pingback.Send(url, parameters); // Verify webRequest1.VerifyAll(); webResponse1.VerifyAll(); webRequest2.VerifyAll(); webResponse2.VerifyAll(); Assert.True(result.Success); Assert.Equal(0, result.Code); Assert.True(String.IsNullOrEmpty(result.Message)); }
protected override string ProcessRequest(string methodName, List <string> parameters) { Uri sourceUri; Uri targetUri; if (!Uri.TryCreate(parameters.First(), UriKind.Absolute, out sourceUri)) { throw new XmlRpcFaultException(16, "The sourceURI was not provided or was not in a valid format."); } if (!Uri.TryCreate(parameters.Last(), UriKind.Absolute, out targetUri)) { throw new XmlRpcFaultException(32, "The targetURI was not provided or was not in a valid format."); } var pageName = targetUri.AbsolutePath.Substring(1); if (pageName.LastIndexOf('/') > 2) { pageName = pageName.Substring(0, pageName.LastIndexOf('/')); } var session = DependencyResolver.Current.GetService <ISession>(); var repository = DependencyResolver.Current.GetService <IRepository>(); var spamChecker = DependencyResolver.Current.GetService <ISpamChecker>(); var transaction = session.BeginTransaction(IsolationLevel.Serializable); try { // Ensure the link is for a page that exists var entry = repository.FindFirstOrDefault(new EntryByNameQuery(pageName)); if (entry == null) { throw new XmlRpcFaultException(32, "The targetURI refers to a page that does not exist."); } // Ensure a pingback doesn't already exist var existing = entry.Pingbacks.FirstOrDefault(x => x.TargetUri.ToLowerInvariant() == sourceUri.ToString().ToLowerInvariant()); if (existing != null) { throw new XmlRpcFaultException(48, "A pingback for this URI has already been registered."); } var pingback = new Pingback { Entry = entry.Entry.Value, TargetUri = sourceUri.ToString().ToLowerInvariant(), TargetTitle = string.Empty, Received = DateTime.UtcNow }; // Check the calling page try { var request = WebRequest.Create(sourceUri); var response = request.GetResponse(); var responseBody = new StreamReader(response.GetResponseStream()).ReadToEnd(); if (!responseBody.Contains(parameters.Last())) { throw new XmlRpcFaultException(17, "The document at the sourceURI does not contain a link to the targetURI"); } var find = Regex.Match(responseBody, "\\<title.*?\\>(.*?)\\</title\\>"); pingback.TargetTitle = find.Success ? HttpContext.Current.Server.HtmlDecode(find.Groups[1].Value) : sourceUri.ToString().ToLower(); } catch (XmlRpcFaultException) { throw; } catch (Exception ex) { throw new XmlRpcFaultException(50, string.Format("The server encountered problems attempting to connect to the sourceURI. {0}", ex.Message)); } // Save the pingback spamChecker.Verify(pingback); entry.Pingbacks.Add(pingback); session.Flush(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } return("Thanks, your pingback has been received."); }
public void Save(Pingback pingback) { session.Update(pingback); }