Re: mod_perl and Apache::Test

Torsten Förtsch <[email protected]>
Newsgroups gmane.comp.apache.mod-perl
Message-ID <[email protected]>
On 10/09/2012 10:44 AM, Jean-Michel wrote:
> I have some mod_perl modules that implement REST services. I want to 
> test them with Apache::Test. So, I have some questions :
> 
> - Is it possible to use DELETE method with Apache::Test ? I didn't
> find any examples

$ perl -MApache::TestRequest -le '
    print Apache::TestRequest::user_agent, " is a ",
          @Apache::TestRequest::ISA
'
Apache::TestRequest=HASH(0x626ab8) is a LWP::UserAgent

So, at least if LWP is installed Apache::TestRequest::user_agent()
returns the user agent object which is a LWP::UserAgent.

That means you can call any method you want, even HUGO:

$ perl -MApache::TestRequest -e '
    print Apache::TestRequest::user_agent->request
      (HTTP::Request->new(HUGO=>"http://opi.home/"))->as_string
'
HTTP/1.1 501 Method Not Implemented
Connection: close
Date: Tue, 09 Oct 2012 09:13:53 GMT
Server: Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/1.0.0e DAV/2
mod_apreq2-20090110/2.8.0 mod_perl/2.0.6threading9 Perl/v5.12.3
Allow: GET,HEAD,POST,OPTIONS,TRACE
Content-Length: 206
Content-Type: text/html; charset=iso-8859-1
Client-Date: Tue, 09 Oct 2012 09:13:53 GMT
Client-Peer: 192.168.0.4:80
Client-Response-Num: 1
Title: 501 Method Not Implemented

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>501 Method Not Implemented</title>
</head><body>
<h1>Method Not Implemented</h1>
<p>HUGO to / not supported.<br />
</p>
</body></html>

> - I want to send JSON data with POST method (not with 
> x-www-form-urlencode). I can do that with cURL, but to do it with 
> Apache::Test ?

Again, HTTP::Request allows you to format almost any request you want:

perl -MApache::TestRequest -e '
    print HTTP::Request->new(HUGO=>"http://opi.home/",
             HTTP::Headers->new(Fritz=>"Fratz"),
             "blah blub"
          )->as_string
'
HUGO http://opi.home/
Fritz: Fratz

blah blub

Unfortunately, you didn't say what you want to use instead of
x-www-form-urlencode. So, I assume you want to use form-data. In that
case this piece of code should do it:

$resp=POST $uri,
           Content_Type=>'form-data',
           Content=>[KEY=>VALUE, ...];

Torsten
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.