karma-runner / karma

Spectacular Test Runner for JavaScript
http://karma-runner.github.io
MIT License
11.96k stars 1.71k forks source link

Karma proxy behaves weirdly #2622

Closed timargra closed 7 years ago

timargra commented 7 years ago

Expected behaviour

Proxy should process requests the same way other servers (e.g. nginx) do.

Actual behaviour

Getting strange errors - sometimes 404, sometimes other unexpected responses - when proxing my APIs (e.g. Mite API) through Karma. When simply proxying http://www.google.com, I get a 404.

To diagnose the problem I proxied an nginx on localhost, but the access log seems fine

Environment Details

karma.config.js

proxies: {
  '/google': 'http://www.google.com',
  '/api/mite/': 'https://yxz.mite.yo.lk/',
  '/base/web/node_modules/': '/base/node_modules/'
},
proxyValidateSSL: false

nginx access.log (first line: direct request, second line: request proxied through Karma)

172.20.0.1 - - [22/Mar/2017:11:44:06 +0000] "GET /api/mite/customers.json?limit=100 HTTP/1.1" 200 8118 "-" "-"
172.20.0.1 - - [22/Mar/2017:11:44:18 +0000] "GET /api/mite/customers.json?limit=100 HTTP/1.1" 404 64 "-" "-"

Steps to reproduce the behaviour

  1. Set up a http proxy in the config
maksimr commented 7 years ago

@timargra you should use changeOrigin option

      '/google': {
        'target': 'http://www.google.com',
        'changeOrigin': true
      },
      '/api/mite/': {
        'target': 'https://yxz.mite.yo.lk/',
        'changeOrigin': true      
       }

The proxy will send Host header which refers to localhost if you don't turn on changeOrigin option.

Thanks!