You are here

public function Ip2CountryResourceTest::testIpLookup in IP-based Determination of a Visitor's Country 8

Makes REST API requests to lookup known-good IP addresses.

File

tests/src/Functional/Ip2CountryResourceTest.php, line 65

Class

Ip2CountryResourceTest
Tests the Ip2Country REST resource.

Namespace

Drupal\Tests\ip2country\Functional

Code

public function testIpLookup() {

  // Real working IPs that are in the database.
  $ip_array = [
    '125.29.33.201' => 'JP',
    '212.58.224.138' => 'GB',
    '184.51.240.110' => 'US',
    '210.87.9.66' => 'AU',
    '93.184.216.119' => 'EU',
  ];

  // Test authentication.
  $this
    ->initAuthentication();
  $url = Url::fromRoute('rest.ip_lookup.GET', [
    'ip_address' => '127.0.0.1',
    '_format' => static::$format,
  ]);
  $request_options = $this
    ->getAuthenticationRequestOptions('GET');
  $response = $this
    ->request('GET', $url, $request_options);
  $this
    ->assertResourceErrorResponse(403, "The 'restful get ip_lookup' permission is required.", $response, [
    '4xx-response',
    'http_response',
  ], [
    'user.permissions',
  ], FALSE, FALSE);

  // Create a user account that has the required permissions to read
  // the ip_lookup resource via the REST API.
  $this
    ->setUpAuthorization('GET');

  // Drupal 9 and Drupal 8 do not have identical responses.
  // @todo Remove this hack when D8 becomes unsupported.
  if (version_compare(\Drupal::VERSION, '9.0', '>=')) {
    $expected_cache_tags = [
      'config:rest.resource.ip_lookup',
      'http_response',
    ];
  }
  else {
    $expected_cache_tags = [
      'config:rest.resource.ip_lookup',
      'config:rest.settings',
      'http_response',
    ];
  }

  // Make requests for known-good IP addresses and verify results.
  foreach ($ip_array as $ip => $country) {
    $url
      ->setRouteParameter('ip_address', $ip);
    $response = $this
      ->request('GET', $url, $request_options);
    $this
      ->assertResourceResponse(200, FALSE, $response, $expected_cache_tags, [
      'user.permissions',
    ], FALSE, 'MISS');
    $lookup_result = Json::decode((string) $response
      ->getBody());
    $this
      ->assertEquals($country, $lookup_result, 'Country code is correct.');
  }

  // Request an unknown IP address.
  // We use a reserved "example" IP address as defined by RFC 5737.
  $url
    ->setRouteParameter('ip_address', '203.0.113.0');
  $response = $this
    ->request('GET', $url, $request_options);
  $this
    ->assertResourceErrorResponse(404, 'IP Address 203.0.113.0 is not assigned to a country.', $response);

  // Make a bad request.
  $url
    ->setRouteParameter('ip_address', 0);
  $response = $this
    ->request('GET', $url, $request_options);
  $this
    ->assertResourceErrorResponse(400, 'The IP address you entered is invalid. Please enter an address in the form xxx.xxx.xxx.xxx where xxx is between 0 and 255 inclusive.', $response);
}