private static ShellModel CreateMazeModel(CompositionContainer container) { EscapeMaze escapeMaze = CreateEscapeMazeMission(); var mazeLink = new Link() {Target = new Uri("http://amundsen.com/examples/mazes/2d/five-by-five/")}; var restAgent = new RestAgent(); MazeSemanticsProvider.RegisterSemantics(restAgent.SemanticsRegistry); restAgent.SemanticsRegistry.RegisterFormatter(new XmlFormatter("application/vnd.amundsen.maze+xml")); restAgent.SemanticsRegistry.RegisterFormatter(new HtmlFormatter()); restAgent.SemanticsRegistry.RegisterLinkExtractor(new HtmlLinkExtractor()); //restAgent.SetAcceptedMediaTypes(new List<MediaTypeWithQualityHeaderValue>(){ new MediaTypeWithQualityHeaderValue("application/vnd.amundsen.maze+xml")}); restAgent.SetAcceptedMediaTypes(new List<MediaTypeWithQualityHeaderValue>() { new MediaTypeWithQualityHeaderValue("text/html") }); restAgent.NavigateTo(mazeLink); return new ShellModel { Container = container, DefaultMission = escapeMaze, RestAgent = restAgent }; }
public void RegisterAUser() { var agent = new RestAgent(); agent.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xhtml+xml")); MicroblogSemanticsProvider.RegisterSemantics(agent.SemanticsRegistry); agent.NavigateTo(new Link() { Target = new Uri("http://127.0.0.1:8080/") }); var content = agent.CurrentContent; var userAddLink = content.GetLink<UserAdd>(); agent.NavigateTo(userAddLink); content = agent.CurrentContent; var userAddForm = content.GetLink<UserAddForm>(); userAddForm.User = "******"; userAddForm.Password = "******"; userAddForm.Email = "*****@*****.**"; userAddForm.Name = "Darrel Miller"; agent.NavigateTo(userAddForm); var finalContent = agent.CurrentContent; // Force request to happen. Assert.AreEqual(HttpStatusCode.OK, agent.CurrentStatusCode); }
public void CreateRestAgent() { //Arrange //Act var agent = new RestAgent(); //Assert Assert.IsNotNull(agent); }
public void GetRootDoc() { var agent = new RestAgent(); agent.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xhtml+xml")); MicroblogSemanticsProvider.RegisterSemantics(agent.SemanticsRegistry); agent.NavigateTo(new Link() { Target = new Uri("http://127.0.0.1:8080/")}); var content = agent.CurrentContent; //Assert.AreEqual("application/xhtml+xml",content.Headers.ContentType); Assert.IsNotNull(content); }
private static ShellModel CreateTestServerModel(CompositionContainer container) { var rootLink = new Link() {Target = new Uri("http://graphite:9000")}; var restAgent = new RestAgent(); restAgent.NavigateTo(rootLink); return new ShellModel { Container = container, RestAgent = restAgent }; }
public void ShouldReturnContentFromRootUrl() { //Arrange var httpClient = new HttpClient(new FakeWebRequestChannel(new StringContent("This is a test"))); var agent = new RestAgent(httpClient); agent.SemanticsRegistry.RegisterFormatter(new PlainTextFormatter()); //Act agent.NavigateTo(new Link() { Target = new Uri("http://example.org/") }); //Assert var textContent = (string)agent.CurrentContent.Value; Assert.AreEqual("This is a test", textContent); Assert.IsNotNull(agent); }
static void Main(string[] args) { var restagent = new RestAgent(); MazeSemanticsProvider.RegisterSemantics(restagent.SemanticsRegistry); restagent.SetAcceptedMediaTypes( new[] { new MediaTypeWithQualityHeaderValue("application/vnd.amundsen.maze+xml") }); var link = new Link() {Target = new Uri("http://amundsen.com/examples/mazes/2d/five-by-five/")}; restagent.NavigateTo(link); var startlink = restagent.CurrentContent.GetLink<StartLink>(); restagent.NavigateTo(startlink); // Pick first available door var firstlink = (from lk in restagent.CurrentContent.GetLinks() where !(lk is CurrentLink || lk is StartLink) select lk).First(); restagent.NavigateTo((Link)firstlink); var linkfrom = firstlink; while (restagent.CurrentContent.GetLink<ExitLink>() == null) { Link chosenLink = null; var availablelinks = (from lk in restagent.CurrentContent.GetLinks() select lk).ToDictionary(lk => lk.Relation); switch(linkfrom.Relation) { case "east": chosenLink = ChooseDoor(availablelinks, "south", "east", "north", "west"); break; case "west": chosenLink = ChooseDoor(availablelinks, "north", "west", "south", "east"); break; case "south": chosenLink = ChooseDoor(availablelinks, "west", "south", "east", "north"); break; case "north": chosenLink = ChooseDoor(availablelinks, "east", "north", "west", "south"); break; } Console.WriteLine("Going : " + chosenLink.Relation); restagent.NavigateTo((Link)chosenLink); linkfrom = chosenLink; } Console.WriteLine("Complete"); Console.ReadLine(); }
public void TestErrorHandling() { // Arrange var fakeResponse = new HttpResponseMessage(HttpStatusCode.InternalServerError); var fakeWebRequestChannel = new FakeWebRequestChannel(fakeResponse); var httpClient = new HttpClient(fakeWebRequestChannel); var agent = new RestAgent(httpClient); // Act agent.NavigateTo(new Link() { Target = new Uri("http://example.org/") }); var content = agent.CurrentContent; // Assert Assert.IsNull(content); Assert.AreEqual(HttpStatusCode.InternalServerError, agent.CurrentStatusCode); }
public void GetALink() { var agent = new RestAgent(); agent.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xhtml+xml")); MicroblogSemanticsProvider.RegisterSemantics(agent.SemanticsRegistry); agent.NavigateTo(new Link() { Target = new Uri("http://127.0.0.1:8080/") }); var content = agent.CurrentContent; var indexLink = content.GetLink<Index>(); var usersAllLink = content.GetLink<UsersAll>(); var userAddLink = content.GetLink<UserAdd>(); var messagesSearchForm = content.GetLink<MessagesSearchForm>(); //Assert.AreEqual("application/xhtml+xml",content.Headers.ContentType); Assert.IsNotNull(indexLink); Assert.IsNotNull(messagesSearchForm); //Assert.IsNotNull(usersAllLink); Assert.IsNotNull(userAddLink); }
public void Go(RestAgent restAgent) { // Find start link and go var startlink = restAgent.CurrentContent.GetLink<StartLink>(); restAgent.NavigateTo(startlink); // Pick first available door var firstlink = (from lk in restAgent.CurrentContent.GetLinks() where "north|south|east|west|exit".Contains(lk.Relation) select lk).First(); restAgent.NavigateTo((Link)firstlink); Link linkfrom = firstlink; while (restAgent.CurrentContent.GetLink<ExitLink>() == null) { Link chosenLink = null; var availablelinks = (from lk in restAgent.CurrentContent.GetLinks() where "north|south|east|west|exit".Contains(lk.Relation) select lk).ToDictionary(lk => lk.Relation); switch(linkfrom.Relation) { case "east": chosenLink = ChooseDoor(availablelinks, "south", "east", "north", "west"); break; case "west": chosenLink = ChooseDoor(availablelinks, "north", "west", "south", "east"); break; case "south": chosenLink = ChooseDoor(availablelinks, "west", "south", "east", "north"); break; case "north": chosenLink = ChooseDoor(availablelinks, "east", "north", "west", "south"); break; } restAgent.NavigateTo((Link)chosenLink); linkfrom = chosenLink; } restAgent.NavigateTo(restAgent.CurrentContent.GetLink<ExitLink>()); }
public void RetweetYuppieMessages() { var agent = new RestAgent(); agent.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xhtml+xml")); MicroblogSemanticsProvider.RegisterSemantics(agent.SemanticsRegistry); agent.NavigateTo(new Link() { Target = new Uri("http://127.0.0.1:8080/") }); agent.SetBasicCredentials("darrel", "foome"); var content = agent.CurrentContent; var messagePostForm = content.GetLink<MessagesSearchForm>(); messagePostForm.Search = "yuppie"; agent.NavigateTo(messagePostForm); var shareFormLinks = agent.CurrentContent.GetLinks<MessageShareForm>(); foreach (var messageShareForm in shareFormLinks) { agent.NavigateTo(messageShareForm); } Assert.AreEqual(HttpStatusCode.OK, agent.CurrentStatusCode); }
public void PostMessage() { var agent = new RestAgent(); agent.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xhtml+xml")); MicroblogSemanticsProvider.RegisterSemantics(agent.SemanticsRegistry); agent.NavigateTo(new Link() { Target = new Uri("http://127.0.0.1:8080/") }); agent.SetBasicCredentials("darrel","foome"); var content = agent.CurrentContent; var messagePostForm = content.GetLink<MessagePostForm>(); messagePostForm.Message = "Here's a message"; agent.NavigateTo(messagePostForm); var finalcontent = agent.CurrentContent; Assert.AreEqual(HttpStatusCode.OK, agent.CurrentStatusCode); }
public RestAgent NavigateToWithChildAgent(Link link) { _ChildAgent = new RestAgent(_HttpClient, _SemanticsRegistry); _ChildAgent._ResponseActions.AddRange(_ResponseActions); _ChildAgent.InternalNavigateTo(link); _ChildAgent.AgentTerminated += () => _ChildAgent = null; return _ChildAgent; }
public void TestRedirectHandling() { // Arrange var responses = new List<HttpResponseMessage>(); var redirectResponse = new HttpResponseMessage(HttpStatusCode.Redirect); redirectResponse.Headers.Location = new Uri("http://example.org/redirectlocation"); responses.Add(redirectResponse); var targetResponse = new HttpResponseMessage(HttpStatusCode.OK); responses.Add(targetResponse); var fakeWebRequestChannel = new FakeWebRequestChannel(responses); var httpClient = new HttpClient(fakeWebRequestChannel); var agent = new RestAgent(httpClient); // Act agent.NavigateTo(new Link() { Target = new Uri("http://example.org/") }); var content = agent.CurrentContent; // Assert Assert.IsNull(content); Assert.AreEqual(HttpStatusCode.OK, agent.CurrentStatusCode); Assert.AreEqual("http://example.org/redirectlocation", agent.CurrentLocation.Target.AbsoluteUri); }