FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Mock HTTP servers for unit tests.
Show: Today's Messages :: Unread Messages :: Polls :: Message Navigator
| Subscribe to topic | Bookmark topic 
Switch to threaded view of this topic Create a new topic Submit Reply
Message by Daniel Pitts is ignored  [reveal message]  [reveal all messages by Daniel Pitts]  [stop ignoring this user] Go to next message
Message by Jerry Stuckle is ignored  [reveal message]  [reveal all messages by Jerry Stuckle]  [stop ignoring this user] Go to previous messageGo to next message
Message by Daniel Pitts is ignored  [reveal message]  [reveal all messages by Daniel Pitts]  [stop ignoring this user] Go to previous messageGo to next message
Message by Jerry Stuckle is ignored  [reveal message]  [reveal all messages by Jerry Stuckle]  [stop ignoring this user] Go to previous messageGo to next message
Message by Daniel Pitts is ignored  [reveal message]  [reveal all messages by Daniel Pitts]  [stop ignoring this user] Go to previous messageGo to next message
Message by Jerry Stuckle is ignored  [reveal message]  [reveal all messages by Jerry Stuckle]  [stop ignoring this user] Go to previous messageGo to next message
Message by Jerry Stuckle is ignored  [reveal message]  [reveal all messages by Jerry Stuckle]  [stop ignoring this user] Go to previous messageGo to next message
Message by Daniel Pitts is ignored  [reveal message]  [reveal all messages by Daniel Pitts]  [stop ignoring this user] Go to previous messageGo to next message
Message by Jerry Stuckle is ignored  [reveal message]  [reveal all messages by Jerry Stuckle]  [stop ignoring this user] Go to previous messageGo to next message
Re: Mock HTTP servers for unit tests. [message #185679 is a reply to message #185676] Wed, 30 April 2014 03:00 Go to previous messageGo to next message
Michael Vilain is currently offline  Michael Vilain
Messages: 88
Registered: September 2010
Karma: 0
Member
add to buddy list
ignore all messages by this user
In article <MzX7v.346104$2N5(dot)318310(at)fx05(dot)iad>,
Daniel Pitts <newsgroup(dot)nospam(at)virtualinfinity(dot)net> wrote:

> On 4/29/14 5:11 PM, Jerry Stuckle wrote:
>> On 4/29/2014 7:56 PM, Daniel Pitts wrote:
>>> On 4/29/14 1:39 PM, Jerry Stuckle wrote:
>>>> On 4/29/2014 4:24 PM, Daniel Pitts wrote:
>>>> > On 4/29/14 11:51 AM, Jerry Stuckle wrote:
>>>> >> On 4/29/2014 1:23 PM, Daniel Pitts wrote:
>>>> >>> I'm developing some code which makes http requests from PHP. I'd
>>>> >>> like to
>>>> >>> be able to Unit Test my code as smoothly as possible.
>>>> >>>
>>>> >>> I've searched Google, and I see there is more than one solution
>>>> >>> available. I like the look of InterNations[1], but I wanted to ask
>>>> >>> this
>>>> >>> community if they've had experience with any of them, and whether
>>>> >>> that
>>>> >>> experience was positive or negative.
>>>> >>>
>>>> >>> Things I need:
>>>> >>> * Ability to validate a specific request was made, with a
>>>> >>> specific
>>>> >>> set of Headers.
>>>> >>> * Ability to provide a "mock" response, and validate my code can
>>>> >>> handle it.
>>>> >>>
>>>> >>> Things I really want:
>>>> >>> * Ability to send parallel requests (think curl_multi), and
>>>> >>> validate
>>>> >>> the requests are made in parallel.
>>>> >>> * Ability to delay one response for a specific amount of time,
>>>> >>> but
>>>> >>> have another response return earlier.
>>>> >>>
>>>> >>> Thanks,
>>>> >>> Daniel.
>>>> >>>
>>>> >>> [1] https://github.com/InterNations/http-mock
>>>> >>
>>>> >> Daniel,
>>>> >>
>>>> >> Normally when testing, I test against the site the code is written
>>>> >> for.
>>>> >> If that's not possible (or I need further information than is
>>>> >> supplied
>>>> >> by the server, as you seem to need), I create my own test site and
>>>> >> write
>>>> >> server-side code to provide the appropriate information.
>>>> > I'm talking about Unit Tests... I need to be able to run them while
>>>> > offline, for instance. Integration tests would in theory be feasible
>>>> > against a site. Note that I support multiple sites and I'm wanting to
>>>> > unit-test some core functionality, not validate integration with any
>>>> > other sites.
>>>>
>>>> Good unit tests are always custom written, and are written against the
>>>> design specs (not the code).
>>>>
>>>> >>
>>>> >> You can see if the requests were made in parallel from the server
>>>> >> logs.
>>>> >> Other information can be validated by the pages being requested.
>>>> > This doesn't solve my underlying problem. I want to write a Unit Test,
>>>> > not an Integration Test. My Unit Test should take a few seconds at
>>>> > most
>>>> > to run, and should not depend on an external service which may be
>>>> > unavailable.
>>>> >
>>>> > Thanks,
>>>> > Daniel.
>>>> >
>>>>
>>>> OK, then you need to write the unit test. Every one I have written in
>>>> many years of programming has been unique, because the code it's testing
>>>> is unique. I've seen drivers out there - but they really don't do much
>>>> more than run a series of tests in sequence; you still have to write the
>>>> individual tests. And I can do the same with a bash shell script (or
>>>> even a PHP or other scripting language script).
>>>>
>>>> And remember to test both valid and invalid values in your unit test.
>>> Very good advice, though it doesn't answer the question nor address the
>>> actual post I made in any way.
>>>
>>> I am very familiar with testing methodologies and technologies. Just
>>> most of my recent experience was with Java, where it's relatively easy
>>> to start a mock http server in a separate thread. Not as easy in PHP.
>>> I'm asking about Mock HTTP servers. Not about testing.
>>>
>>> Thanks,
>>> Daniel.
>>
>> P.S. I create a test server no matter whether the test is for java, php,
>> .net, python or any other web scripting language.
>>
>> But what you're looking for does not sound much like unit test. It
>> sounds more like module test - which requires a test server for the
>> client side of client-server scripts.
>
> You do this nearly every single time I post to this newsgroup. You
> don't understand my requirements, and I do. Stop trying to read into
> it. I asked about Mock server, and provide an example that looks like
> it should work. I have asked for experience with said mock server, or
> other similar systems.

Well, seems you've been here before and gotten the same answer to your
question. You pose a very specific question. You got no answer other
than "here's what I do. Why are you doing that instead?"

Not what you're looking for? Time to look elsewhere. If all you want
is confirmation (or applause) then you're come to the wrong place. Try
contacting the developer of the tool you have questions about. They'll
probably have more info than us. You want to re-invent the wheel or do
testing your way, fine. Ask the developer for help would probably be a
better use of your time (and ours).

--
DeeDee, don't press that button! DeeDee! NO! Dee...
[I filter all Goggle Groups posts, so any reply may be automatically ignored]
Re: Mock HTTP servers for unit tests. [message #185681 is a reply to message #185673] Wed, 30 April 2014 10:28 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
remove from buddy list
ignore all messages by this user
On Tue, 29 Apr 2014 16:56:57 -0700, Daniel Pitts wrote:

> I am very familiar with testing methodologies and technologies. Just
> most of my recent experience was with Java, where it's relatively easy
> to start a mock http server in a separate thread. Not as easy in PHP.
> I'm asking about Mock HTTP servers. Not about testing.

What do you want your mock http server to do? It probably isn't going to
be able to sit on port 80 without privs, assuming nothing else is on port
80 already. Does it need to do more than open a listening port on
localhost, listen for some packets, send responses depending on the
requests? I believe php comes with a basic http server, perhaps you could
adapt that to your needs?

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Message by Jerry Stuckle is ignored  [reveal message]  [reveal all messages by Jerry Stuckle]  [stop ignoring this user] Go to previous messageGo to next message
Message by Daniel Pitts is ignored  [reveal message]  [reveal all messages by Daniel Pitts]  [stop ignoring this user] Go to previous messageGo to next message
Message by Daniel Pitts is ignored  [reveal message]  [reveal all messages by Daniel Pitts]  [stop ignoring this user] Go to previous messageGo to next message
Message by Daniel Pitts is ignored  [reveal message]  [reveal all messages by Daniel Pitts]  [stop ignoring this user] Go to previous messageGo to next message
Message by Jerry Stuckle is ignored  [reveal message]  [reveal all messages by Jerry Stuckle]  [stop ignoring this user] Go to previous messageGo to next message
Message by Jerry Stuckle is ignored  [reveal message]  [reveal all messages by Jerry Stuckle]  [stop ignoring this user] Go to previous messageGo to next message
Message by Christoph Michael Bec is ignored  [reveal message]  [reveal all messages by Christoph Michael Bec]  [stop ignoring this user] Go to previous messageGo to next message
Message by Christoph Michael Bec is ignored  [reveal message]  [reveal all messages by Christoph Michael Bec]  [stop ignoring this user] Go to previous messageGo to next message
Re: Mock HTTP servers for unit tests. [message #185693 is a reply to message #185686] Wed, 30 April 2014 15:46 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 30/04/14 17:40, Daniel Pitts wrote:
> I tell you none of this to receive validation or kudos. Only to let you
> know the context of my question. I'm not budding young programmer; I'm
> an experienced veteran. When I say I want a Mock HTTP server, I know
> that's exactly what I want, and it will do what I want.

I would say that you probably need to write it.

Most people simply set up apache on a spare linux machne. If that
doesn't have the fine control you need, either hack te source or start
again using bits of it as a starting point


--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
Re: Mock HTTP servers for unit tests. [message #185694 is a reply to message #185683] Wed, 30 April 2014 21:47 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
remove from buddy list
ignore all messages by this user
On Wed, 30 Apr 2014 11:34:15 -0400, Jerry Stuckle wrote:

> On 4/30/2014 10:28 AM, Denis McMahon wrote:
>> On Tue, 29 Apr 2014 16:56:57 -0700, Daniel Pitts wrote:

>>> I am very familiar with testing methodologies and technologies. Just
>>> most of my recent experience was with Java, where it's relatively easy
>>> to start a mock http server in a separate thread. Not as easy in PHP.
>>> I'm asking about Mock HTTP servers. Not about testing.

>> What do you want your mock http server to do? It probably isn't going
>> to be able to sit on port 80 without privs, assuming nothing else is on
>> port 80 already. Does it need to do more than open a listening port on
>> localhost, listen for some packets, send responses depending on the
>> requests? I believe php comes with a basic http server, perhaps you
>> could adapt that to your needs?

> Denis,
> Why bother, when it's so easy to set up an Apache test system?

Hey Jerry, the way he wants to do it isn't the way I'd do it - if I
wanted to test my code against some web-server server-side functionality,
I'd implement the server function in my server and then test my code
against it.

But for unit test, I'd normally write a wrapper that delivered the dummy
test http responses into the code that was expected to process them. If I
was making calls to another webserver I'd probably be using curl which is
generally mature code and in my experience tested well enough by its own
developers that I can pretty reliably take it that it will accurately
handle its part of the job.

That means I really don't need to worry about testing the curl / remote
server / curl part of anything I develop, I just have to check that the
request data that would go to curl is right, and that what I expect to
come back from curl would get correctly processed, and for that purpose
the boundary of the code I am testing would be the interface to the curl
calls.

Of course it may be that he wants to test the remote server part of the
application as well, but if he's trying to do that with a mock server
he's screwing his tests over big time, because the only correct way to
test the remote server part of the application is by running it as part
of a full blown server, not using some pseudo server.

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Message by Jerry Stuckle is ignored  [reveal message]  [reveal all messages by Jerry Stuckle]  [stop ignoring this user] Go to previous messageGo to next message
Re: Mock HTTP servers for unit tests. [message #185696 is a reply to message #185668] Thu, 01 May 2014 07:54 Go to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
Daniel Pitts wrote:

> I'm developing some code which makes http requests from PHP. I'd like to
> be able to Unit Test my code as smoothly as possible.
> […]
> Things I need:
> * Ability to validate a specific request was made, with a specific
> set of Headers.
> * Ability to provide a "mock" response, and validate my code can
> handle it.
>
> Things I really want:
> * Ability to send parallel requests (think curl_multi), and validate
> the requests are made in parallel.
> * Ability to delay one response for a specific amount of time, but
> have another response return earlier.

PHP 5.4+ and “php -S $TEST_HOST:$PORT your_handler.php”, perhaps with the
“-c” and/or “-d” options, should suffice.

Depending on the header fields you need to read, and the PHP version you
have available, you might need to use a different server.
apache_request_headers() is available in the CLI server since PHP 5.5.7.

I have verified that the CLI server of PHP 5.5.11 can handle several
requests made at approximately the same time (as expected):

,----
| $ echo '<?php echo $_SERVER["REQUEST_URI"] . "\n";' > server.php
| $ php -S localhost:1337 server.php
| Listening on http://localhost:1337
| Document root is /home/****/scripts/php/test
| Press Ctrl-C to quit.
`----

In another terminal:

,----
| $ GET http://localhost:1337/foo & GET http://localhost:1337/bar
| [1] 19301
| /bar
| $ /foo
| [1]+ Done GET http://localhost:1337/foo
`----

[“GET” is symlinked to lwp-request(1p), a Perl-based command-line tool to
make HTTP requests. The “$” in-between is only the client's shell
interfering with the server output; you will not see it if you use a PHP
client like curl_multi(). Note that apparently reading the server script
from a FIFO is not possible; you need to create a real file.]

<http://php.net/manual/en/features.commandline.webserver.php>
<http://php.net/manual/en/reserved.variables.php>
<http://php.net/apache_request_headers>
<http://php.net/sleep>


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Quick Reply
Formatting Tools:   
  Switch to threaded view of this topic Create a new topic
Previous Topic: Re: Disaster Mitigation Program - Apr. 27, 2014
Next Topic: Storing strings and numbers properly in CSV files
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Apr 16 21:06:34 EDT 2024

Total time taken to generate the page: 0.04831 seconds