Remote-Connection.t 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. use strict;
  2. use warnings;
  3. use Test::More;
  4. use Test::Fatal;
  5. use Test::LWP::UserAgent;
  6. BEGIN: {
  7. unless (use_ok('Selenium::Remote::RemoteConnection')) {
  8. BAIL_OUT("Couldn't load Selenium::Remote::RemoteConnection");
  9. exit;
  10. }
  11. }
  12. ABSOLUTE_PATH_REDIRECTS: {
  13. my $tua = Test::LWP::UserAgent->new(max_redirect => 0);
  14. $tua->map_response(qr/redirect/, HTTP::Response->new(303, undef, ['Location' => '/elsewhere']));
  15. $tua->map_response(qr/^http:\/\/localhost:80\/elsewhere$/, HTTP::Response->new(
  16. 200,
  17. 'OK',
  18. ['Content-Type' => 'application/json'],
  19. ''
  20. ));
  21. my $conn = Selenium::Remote::RemoteConnection->new(
  22. remote_server_addr => 'localhost',
  23. port => '80',
  24. ua => $tua
  25. );
  26. my $redirect_endpoint = {
  27. method => 'GET',
  28. url => 'http://localhost/redirect'
  29. };
  30. my $resp = $conn->request($redirect_endpoint);
  31. is($resp->{cmd_status}, 'OK', 'can redirect to absolute path');
  32. }
  33. REDIRECT: {
  34. my $tua = Test::LWP::UserAgent->new(
  35. max_redirect => 0
  36. );
  37. $tua->map_response(qr/redirect/, HTTP::Response->new(303, undef, ['Location' => 'http://localhost/elsewhere']));
  38. $tua->map_response(qr/elsewhere/, HTTP::Response->new(200, 'OK', undef, ''));
  39. my $conn = Selenium::Remote::RemoteConnection->new(
  40. remote_server_addr => 'localhost',
  41. port => '',
  42. ua => $tua
  43. );
  44. my $redirect_endpoint = {
  45. method => 'GET',
  46. url => 'http://localhost/redirect'
  47. };
  48. is( exception { $conn->request($redirect_endpoint) }, undef,
  49. '303 redirects no longer kill us');
  50. }
  51. WD_CONTEXT: {
  52. my $tua = Test::LWP::UserAgent->new;
  53. my $hit_custom_context = 0;
  54. my $response = sub {
  55. is($_[0]->uri, 'http://addr:port/test/anything', 'can construct url with custom wd_context' );
  56. $hit_custom_context++;
  57. return HTTP::Response->new(200, 'OK', undef, '')
  58. };
  59. $tua->map_response(qr/test/, $response);
  60. my $conn = Selenium::Remote::RemoteConnection->new(
  61. remote_server_addr => 'addr',
  62. port => 'port',
  63. wd_context_prefix => '/test',
  64. ua => $tua
  65. );
  66. my $endpoint = { method => 'GET', url => 'anything' };
  67. $conn->request($endpoint);
  68. ok($hit_custom_context, 'wd_context is set up properly');
  69. }
  70. done_testing;