DEV Community

Mohammad Waseem
Mohammad Waseem

Posted on

Overcoming Geo-Blocking in Microservices: A JavaScript-Based Solution

Introduction

In modern microservices architectures, deploying geographically restricted features poses unique challenges, especially when automated testing is involved. Geo-blocking—restricting access based on user location—can be particularly tricky to test consistently, as real-world placement and IP-based detection can hinder repeatable, isolated tests.

This article explores a robust approach to testing geo-blocked features within a JavaScript environment, leveraging middleware and mocking techniques to simulate geographic conditions during automated testing.

The Challenge

Imagine a payment service feature that is only available in certain regions. During development and CI/CD pipelines, testing such features becomes complicated because actual IP geolocation often depends on real network environments or third-party services, making tests flaky and difficult to manage.

The goal is to simulate geographic conditions reliably within our unit and integration tests, without depending on external services or network configurations.

Architectural Approach

Our solution involves creating a custom middleware layer that can override or mock geolocation detection during tests. In production, this middleware uses an external API or IP-based lookup service. During testing, it reads from a controlled environment variable or test-specific configuration.

Middleware Example

// geoMiddleware.js
export function geoMiddleware(req, res, next) {
  if (process.env.TEST_GEOBLOCK_OVERRIDE) {
    // During tests, use test-specific override
    req.geolocation = JSON.parse(process.env.TEST_GEOBLOCK_OVERRIDE);
  } else {
    // In production, perform real geolocation lookup here
    // Example placeholder for IP geolocation API
    req.geolocation = getGeolocationFromIP(req.ip);
  }
  next();
}
Enter fullscreen mode Exit fullscreen mode

This middleware checks for an environment variable indicating test mode and sets the req.geolocation property accordingly.

Mocking Geolocation in Tests

In your test setup, you can set the environment variable to simulate specific regions:

// test/setup.js
process.env.TEST_GEOBLOCK_OVERRIDE = JSON.stringify({ country: 'US', region: 'California' });
Enter fullscreen mode Exit fullscreen mode

And your tests then verify feature availability based on this mocked location:

// test/feature.test.js
import request from 'supertest';
import app from '../app';

describe('Geo-restricted feature', () => {
  it('enables feature for US location', async () => {
    process.env.TEST_GEOBLOCK_OVERRIDE = JSON.stringify({ country: 'US' });
    const response = await request(app).get('/feature-endpoint');
    expect(response.status).toBe(200);
    expect(response.body).toHaveProperty('featureAvailable', true);
  });

  it('disables feature for non-allowed country', async () => {
    process.env.TEST_GEOBLOCK_OVERRIDE = JSON.stringify({ country: 'FR' });
    const response = await request(app).get('/feature-endpoint');
    expect(response.status).toBe(403);
    expect(response.body).toHaveProperty('error', 'Region not supported');
  });
});
Enter fullscreen mode Exit fullscreen mode

Best Practices

  • Isolation: Always isolate geo-block simulation within your test environment to avoid affecting production.
  • Configurability: Use environment variables or configuration files to toggle geolocation overrides.
  • Layered Validation: Validate both the middleware behavior and the feature logic depending on the geolocation data.
  • End-to-End Testing: Combine mock-based tests with real API tests in controlled environments.

Conclusion

Testing geo-blocked features in a microservices architecture becomes manageable when you abstract the geolocation logic behind middleware and leverage environment-specific overrides. This approach ensures reliable, repeatable tests while maintaining flexibility for real-world deployment.

By implementing such mocking strategies, architects and developers can confidently develop, test, and deploy geographically restricted features without compromising on quality or speed.


🛠️ QA Tip

Pro Tip: Use TempoMail USA for generating disposable test accounts.

Top comments (0)