Skip to content

Commit

Permalink
better header tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 19, 2012
1 parent 8cac005 commit 4815ea9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -7,6 +7,7 @@
- Added n function to ojo.
- Improved text_field helper to always set the type attribute. (marty, sri)
- Improved documentation.
- Improved tests.

3.11 2012-07-19
- Added or method to Test::Mojo. (moritz, sri)
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -98,10 +98,10 @@ sub form {

sub json {
my ($self, $url, $data) = (shift, shift, shift);
my $headers = ref $_[0] eq 'HASH' ? $_[0] : {@_};
$headers->{'Content-Type'} //= 'application/json';
return $self->tx(POST => $url, $headers,
$self->json_class->new->encode($data));
my $tx = $self->tx(POST => $url, @_, $self->json_class->new->encode($data));
my $headers = $tx->req->headers;
$headers->content_type('application/json') unless $headers->content_type;
return $tx;
}

# "This kid's a wonder!
Expand Down
9 changes: 8 additions & 1 deletion t/mojo/headers.t
@@ -1,6 +1,6 @@
use Mojo::Base -strict;

use Test::More tests => 87;
use Test::More tests => 89;

# "So, have a merry Christmas, a happy Hanukkah, a kwaazy Kwanza,
# a tip-top Tet, and a solemn, dignified, Ramadan.
Expand Down Expand Up @@ -41,6 +41,12 @@ $headers->status('200 OK');
is $headers->status, '200 OK', 'right value';
is $headers->header('Status'), $headers->status, 'values are equal';

# Mixed case
is $headers->header('content-type' => 'text/plain')->content_type,
'text/plain', 'right value';
is $headers->header('Content-Type' => 'text/html')->content_type, 'text/html',
'right value';

# Common headers
$headers = Mojo::Headers->new;
is $headers->accept('foo')->accept, 'foo', 'right value';
Expand Down Expand Up @@ -147,6 +153,7 @@ is_deeply $headers->to_hash,
$headers->from_hash({});
is_deeply $headers->to_hash, {}, 'right structure';

# Multiline
$headers = Mojo::Headers->new;
$headers->from_hash(
{'X-Test' => [[23, 24], ['single line'], [25, 26]], 'X-Test2' => 'foo'});
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/transactor.t
Expand Up @@ -68,7 +68,7 @@ is_deeply $tx->req->json, {test => 123}, 'right content';

# JSON POST with custom content type
$tx = $t->json('http://kraih.com/foo', => [1, 2, 3] =>
{DNT => 1, 'Content-Type' => 'application/something'});
{DNT => 1, 'content-type' => 'application/something'});
is $tx->req->url->to_abs, 'http://kraih.com/foo', 'right URL';
is $tx->req->method, 'POST', 'right method';
is $tx->req->headers->dnt, 1, 'right "DNT" value';
Expand Down

0 comments on commit 4815ea9

Please sign in to comment.