dredd icon indicating copy to clipboard operation
dredd copied to clipboard

Error connecting to server under test when server on localhost

Open karpilin opened this issue 9 years ago • 20 comments

Describe your problem

Each test is failing with the following error:

error: Error connecting to server under test!

Despite the api endpoint is set to http://localhost:8000 , the api calls are sent to http://127.0.0.1:8000.

Running a web server on 127.0.0.1 instead of localhost solves the problem.

What command line options do you use?

$ dredd

What is in your dredd.yml?

reporter: apiary
custom:
  apiaryApiKey: *******
  apiaryApiName: ********
dry-run: null
hookfiles: './hooks.php'
language: 'bin/dredd-hooks-php'
sandbox: false
server:
server-wait: 3
init: false
names: false
only: []
output: []
header: []
sorted: false
user: null
inline-errors: false
details: false
method: []
color: true
level: info
timestamp: false
silent: false
path: []
hooks-worker-timeout: 5000
hooks-worker-connect-timeout: 1500
hooks-worker-connect-retry: 500
hooks-worker-after-connect-wait: 100
hooks-worker-term-timeout: 5000
hooks-worker-term-retry: 500
hooks-worker-handler-host: localhost
hooks-worker-handler-port: 61321
config: ./dredd.yml
blueprint: api.apib
endpoint: 'http://localhost:8000/api/v1'

What's your dredd --version output?

dredd v2.2.5 (Darwin 15.6.0; x64)

Does dredd --level=debug uncover something?

debug: Evaluating results of transaction execution #1: Authorisation > Logout > Logout
debug: No emission of test data to reporters { Error: connect ECONNREFUSED 127.0.0.1:8000
    at Object.exports._errnoException (util.js:1028:11)
    at exports._exceptionWithHostPort (util.js:1051:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)

  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 8000 }

karpilin avatar Feb 24 '17 14:02 karpilin

Thanks for filing this! I've bumped into a similar problem when working on Windows support, but it's clear from your logs that you're using macOS. This needs tests and further investigation. Are you sure your computer correctly resolves localhost to an IP address?

honzajavorek avatar Feb 27 '17 14:02 honzajavorek

@karpilin Could you please verify localhost is directed to 127.0.0.1 in your /etc/hosts file?

honzajavorek avatar Mar 03 '17 15:03 honzajavorek

@karpilin I think this might be the problem you're experiencing: https://github.com/apiaryio/dredd/issues/586

honzajavorek avatar Mar 08 '17 14:03 honzajavorek

@honzajavorek thanks for looking at it.

here hosts file:

$ cat /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost ```

karpilin avatar Mar 10 '17 10:03 karpilin

@karpilin Thanks! How does the command, which you use to run the server, look like? Or maybe you use standalone server, such as Apache?

honzajavorek avatar Mar 15 '17 10:03 honzajavorek

@honzajavorek I run symfony build-in server

$ bin/console s:r localhost:8000

                                                                                                                        
 [OK] Server running on http://localhost:8000                                                                           
                                                                                                                        

 // Quit the server with CONTROL-C.                                                                                     

PHP 5.6.29 Development Server started at Wed Mar 15 11:20:18 2017
Listening on http://localhost:8000
Document root is ***
Press Ctrl-C to quit.

karpilin avatar Mar 15 '17 13:03 karpilin

Same problem here! I use server: 'php -S 127.0.0.1:3000 -t public'

Pablodotnet avatar Apr 12 '17 22:04 Pablodotnet

@Pablodotnet @karpilin So you say this is not related to 127.0.0.1 vs. localhost routing? The 127.0.0.1 doesn't work as well? 😞

honzajavorek avatar Apr 13 '17 08:04 honzajavorek

@honzajavorek it actually DOES work when I start a php server on 127.0.0.1 instead of localhost.

the problem is that despite the api endpoint is set to http://localhost:8000 , the api calls are sent to http://127.0.0.1:8000.

karpilin avatar May 16 '17 11:05 karpilin

Then I think this is really the same issue as #586. localhost is just a placeholder for an IP address, but the problem is, in IPv4 the meaning of the placeholder is different then in IPv6.

From what you describe, it seems the php server resolves the word localhost to the IPv6 address (::1), while Dredd resolves localhost to the IPv4 address (127.0.0.1). That's why it works when you force the php server to the IPv4 address.

I'm merging the two issues together. In the https://github.com/apiaryio/dredd/issues/586#issuecomment-286123492 I proposed several solutions. Feel free to comment on what you think would be the best.

honzajavorek avatar May 16 '17 12:05 honzajavorek

hi @honzajavorek can we connect to public api endpoint instead of local host using dredd?

sajantha avatar Apr 19 '18 13:04 sajantha

Hello @sajantha, absolutely! Provided the server location will use valid URL scheme, it will work as expected.

michalholasek avatar Apr 19 '18 13:04 michalholasek

thanks @michalholasek ,but sorry i am completely new to this. Can u pls give me the example of a valid URL scheme. The end point i tried to connect is a aws one which somewhat looks like this xxxxxxx.xxxxxxx-xx.eu-west-1.amazonaws.com .Is it a valid call

sajantha avatar Apr 19 '18 13:04 sajantha

In the context of server location it is protocol://hostname, eg. https://example.com/. Add a protocol (I presume you are using http, so http://xxxxxxx.xxxxxxx-xx.eu-west-1.amazonaws.com) and it will be valid server location.

michalholasek avatar Apr 19 '18 14:04 michalholasek

Thanks michal

On Thu, Apr 19, 2018, 7:50 PM Michal Holasek [email protected] wrote:

In the context of server location it is protocol://hostname, eg. https://example.com/. Add a protocol (I presume you are using http, so http://xxxxxxx.xxxxxxx-xx.eu-west-1.amazonaws.com) and it will be valid server location.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/apiaryio/dredd/issues/721#issuecomment-382754805, or mute the thread https://github.com/notifications/unsubscribe-auth/AYH6zn0O8lqeEFp-Wlg1IHk6tztNmqWGks5tqJ0ogaJpZM4MLS5q .

sajantha avatar Apr 19 '18 14:04 sajantha

In both of the cases using 127.0.0.1 and localhost, not working. Here are the config file details

module.exports = { port: 8000, dbUser: 'XXXX', dbPassword: 'XXXX', dbHost: 'localhost', db: 'XXXXX', serverURL: 'http://127.0.0.1:8000' }

Error: connect ECONNREFUSED 127.0.0.1:8000 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1170:14)

arpitbhatt027 avatar Mar 11 '19 06:03 arpitbhatt027

@arpitbhatt027 Do other clients work with the same address? Like browser, curl

honzajavorek avatar Mar 11 '19 10:03 honzajavorek

@honzajavorek yes

arpitbhatt027 avatar Mar 11 '19 11:03 arpitbhatt027

Could you please file a new issue with the whole Dredd output with --loglevel=debug? Thanks!

honzajavorek avatar Mar 11 '19 11:03 honzajavorek

I have the same error. And I forgot to on my wamp server. Thanks :)

khyatibhojawala avatar Jan 02 '20 06:01 khyatibhojawala