Skip to content

Commit

Permalink
better JSON POST examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 19, 2012
1 parent 369c1bd commit 01816db
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
26 changes: 12 additions & 14 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -53,7 +53,7 @@ sub form {
elsif (ref $value eq 'HASH') {

# Enforce "multipart/form-data"
$multipart = 1;
$multipart++;

# File
if (my $file = $value->{file}) {
Expand All @@ -75,11 +75,11 @@ sub form {
}

# New transaction
my $tx = $self->tx(POST => $url);
my $req = $tx->req;
my $headers = $req->headers->from_hash(ref $_[0] eq 'HASH' ? $_[0] : {@_});
my $tx = $self->tx(POST => $url, @_);

# Multipart
my $req = $tx->req;
my $headers = $req->headers;
$headers->content_type('multipart/form-data') if $multipart;
if (($headers->content_type || '') eq 'multipart/form-data') {
my $parts = $self->_multipart($encoding, $p->to_hash);
Expand Down Expand Up @@ -166,20 +166,18 @@ sub redirect {
sub tx {
my $self = shift;

# New transaction
# Method and URL
my $tx = Mojo::Transaction::HTTP->new;
my $req = $tx->req;
$req->method(shift);
my $req = $tx->req->method(shift);
my $url = shift;
$url = "http://$url" unless $url =~ m!^/|\://!;
ref $url ? $req->url($url) : $req->url->parse($url);

# Body
$req->body(pop)
if @_ & 1 == 1 && ref $_[0] ne 'HASH' || ref $_[-2] eq 'HASH';

# Headers
$req->headers->from_hash(ref $_[0] eq 'HASH' ? $_[0] : {@_});
$req->headers->from_hash(shift) if ref $_[0] eq 'HASH';

# Body
$req->body(shift) if @_;

return $tx;
}
Expand All @@ -188,7 +186,7 @@ sub tx {
sub websocket {
my $self = shift;

# New WebSocket
# New WebSocket transaction
my $tx = $self->tx(GET => @_);
my $req = $tx->req;
my $abs = $req->url->to_abs;
Expand Down Expand Up @@ -349,7 +347,7 @@ enforce it by setting the header manually.
Versatile L<Mojo::Transaction::HTTP> builder for C<POST> requests with JSON
data.
# Change method to PATCH
# Change method
my $tx = $t->json('mojolicio.us/hello', {hello => 'world'});
$tx->req->method('PATCH');
Expand Down
17 changes: 10 additions & 7 deletions lib/Test/Mojo.pm
Expand Up @@ -201,16 +201,14 @@ sub post_ok { shift->_request_ok(post => @_) }

sub post_form_ok {
my ($self, $url) = (shift, shift);
$self->tx($self->ua->post_form($url, @_));
return $self->_test('ok', $self->tx->is_finished,
encode('UTF-8', "post $url"));
my $tx = $self->tx($self->ua->post_form($url, @_))->tx;
return $self->_test('ok', $tx->is_finished, encode('UTF-8', "post $url"));
}

sub post_json_ok {
my ($self, $url) = (shift, shift);
$self->tx($self->ua->post_json($url, @_));
return $self->_test('ok', $self->tx->is_finished,
encode('UTF-8', "post $url"));
my $tx = $self->tx($self->ua->post_json($url, @_))->tx;
return $self->_test('ok', $tx->is_finished, encode('UTF-8', "post $url"));
}

# "WHO IS FONZY!?! Don't they teach you anything at school?"
Expand Down Expand Up @@ -309,7 +307,7 @@ sub _request_ok {
$headers = {} if !ref $headers;

# Perform request against application
$self->tx($self->ua->$method($url, %$headers, $body));
$self->tx($self->ua->$method($url, $headers, $body));
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ($err, $code) = $self->tx->error;
Test::More::diag $err if !(my $ok = !$err || $code) && $err;
Expand Down Expand Up @@ -677,6 +675,11 @@ the exact same arguments as L<Mojo::UserAgent/"post_form">.
Perform a C<POST> request with JSON data and check for transport errors, takes
the exact same arguments as L<Mojo::UserAgent/"post_json">.
# Test JSON API
$t->post_json_ok('/hello.json' => {hello => 'world'})
->status_is(200)
->json_content_is({bye => 'world'});
=head2 C<put_ok>
$t = $t->put_ok('/foo');
Expand Down

0 comments on commit 01816db

Please sign in to comment.