line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::WWW::Mechanize::Catalyst::WithContext; |
2
|
4
|
|
|
4
|
|
163655
|
use 5.008001; |
|
4
|
|
|
|
|
13
|
|
3
|
4
|
|
|
4
|
|
1379
|
use Moose; |
|
4
|
|
|
|
|
1536423
|
|
|
4
|
|
|
|
|
35
|
|
4
|
4
|
|
|
4
|
|
27164
|
use HTTP::Request; |
|
4
|
|
|
|
|
60042
|
|
|
4
|
|
|
|
|
114
|
|
5
|
4
|
|
|
4
|
|
28
|
use Carp 'croak'; |
|
4
|
|
|
|
|
13
|
|
|
4
|
|
|
|
|
985
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Catalyst::Test; |
8
|
|
|
|
|
|
|
extends 'Test::WWW::Mechanize::Catalyst'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = "0.02"; |
11
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# this stores the ctx_request function as a code reference |
14
|
|
|
|
|
|
|
has _get_context => ( |
15
|
|
|
|
|
|
|
is => 'ro', |
16
|
|
|
|
|
|
|
isa => 'CodeRef', |
17
|
|
|
|
|
|
|
lazy => 1, |
18
|
|
|
|
|
|
|
builder => '_build__get_context', |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub _build__get_context { |
22
|
2
|
|
|
2
|
|
6
|
my ($self) = @_; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# we need $request for ctx_request |
25
|
|
|
|
|
|
|
my $request = Catalyst::Test::_build_request_export( |
26
|
|
|
|
|
|
|
undef, # this is C::T's $self |
27
|
|
|
|
|
|
|
{ class => $self->{catalyst_app}, remote => $ENV{CATALYST_SERVER} } |
28
|
2
|
|
|
|
|
14
|
); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
return Catalyst::Test::_build_ctx_request_export( |
31
|
|
|
|
|
|
|
undef, # this is C::T's $self |
32
|
|
|
|
|
|
|
{ |
33
|
|
|
|
|
|
|
class => $self->{catalyst_app}, |
34
|
2
|
|
|
|
|
120
|
request => $request, |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub get_context { |
40
|
4
|
|
|
4
|
1
|
53537
|
my ( $self, $url ) = @_; |
41
|
|
|
|
|
|
|
|
42
|
4
|
100
|
|
|
|
27
|
croak 'url is required' unless $url; |
43
|
|
|
|
|
|
|
|
44
|
3
|
|
100
|
|
|
19
|
my $request = HTTP::Request->new( GET => URI->new_abs( $url, $self->base || 'http://localhost' ) ); |
45
|
3
|
|
|
|
|
1438
|
$self->cookie_jar->add_cookie_header($request); |
46
|
|
|
|
|
|
|
|
47
|
3
|
|
|
|
|
857
|
my ( $res, $c ) = $self->_get_context->($request); |
48
|
|
|
|
|
|
|
|
49
|
3
|
|
|
|
|
155165
|
return $res, $c; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
1; |
53
|
|
|
|
|
|
|
__END__ |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=encoding utf-8 |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 NAME |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Test::WWW::Mechanize::Catalyst::WithContext - T::W::M::C can now give you $c |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=begin html |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
<p> |
64
|
|
|
|
|
|
|
<a href="https://travis-ci.org/simbabque/Test-WWW-Mechanize-Catalyst-WithContext"><img src="https://travis-ci.org/simbabque/Test-WWW-Mechanize-Catalyst-WithContext.svg?branch=master"></a> |
65
|
|
|
|
|
|
|
<a href='https://coveralls.io/github/simbabque/Test-WWW-Mechanize-Catalyst-WithContext?branch=master'><img src='https://coveralls.io/repos/github/simbabque/Test-WWW-Mechanize-Catalyst-WithContext/badge.svg?branch=master' alt='Coverage Status' /></a> |
66
|
|
|
|
|
|
|
</p> |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=end html |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 VERSION |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Version 0.02 |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 SYNOPSIS |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
use Test::WWW::Mechanize::Catalyst::WithContext; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
my $mech = Test::WWW::Mechanize::Catalyst::WithContext->new( catalyst_app => 'Catty' ); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
my ($res, $c) = $mech->get_context("/"); # $c is a Catalyst context |
81
|
|
|
|
|
|
|
is $c->stash->{foo}, "bar", "foo got set to bar"; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
$mech->post_ok("login", { u => "test", p => "secret" }); |
84
|
|
|
|
|
|
|
my ($res, $c) = $mech->get_context("/"); |
85
|
|
|
|
|
|
|
is $c->session->{stuff}, "something", "things are in the session"; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 DESCRIPTION |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Test::WWW::Mechanize::Catalyst::WithContext is a subclass of L<Test::WWW::Mechanize::Catalyst> |
90
|
|
|
|
|
|
|
that can give you the C<$c> context object of the request you just did. This is useful for |
91
|
|
|
|
|
|
|
testing if things ended up in the stash correctly, if the session got filled without reaching |
92
|
|
|
|
|
|
|
into the persistence layer or to grab an instance of a model, view or controller to do tests |
93
|
|
|
|
|
|
|
on them. Since the cookie jar of your C<$mech> will be used to fetch the context, things |
94
|
|
|
|
|
|
|
like being logged into your app will be taken into account. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Besides that, it's just the same as L<Test::WWW::Mechanize::Catalyst>. It inherits everything |
97
|
|
|
|
|
|
|
and does not overwrite any functionality. See the docs of L<Test::WWW::Mechanize::Catalyst> for |
98
|
|
|
|
|
|
|
more details. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 METHODS |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head2 get_context($url) |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Does a GET request on C<$url> and returns the L<HTTP::Response> and the request context C<$c>. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my ( $res, $c ) = $mech->get_context('/'); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
This is not a C<get_ok> and does not create test output. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 EXAMPLES |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
The following section gives a few examples where it's useful to have C<$c>. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head2 Are we loading the right template? |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
If the content that comes out of your application does not really contain any distinct markers |
117
|
|
|
|
|
|
|
it's very hard to check if the right stuff got rendered. Instead of trying to find something |
118
|
|
|
|
|
|
|
in your HTML that helps you identify the right page with one of the content checking methods like |
119
|
|
|
|
|
|
|
C<content_like>, you can just look at the template name in the stash. Of course that doesn't tell |
120
|
|
|
|
|
|
|
you if it got rendered successfully, but it does tell you which template the controller decided |
121
|
|
|
|
|
|
|
should be rendered. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
my ( $res, $c ) = $mech->get_context('/hard/to/verify/page); |
124
|
|
|
|
|
|
|
is $c->stash->{template}, 'hard_to_verify.tt2', 'the right template got selected'; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head2 Checking what's in the session without talking to the store |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
If you want to look at values in the session before and after some action, you would typically |
129
|
|
|
|
|
|
|
go and connect to the session store and peek around. For that, you need to know the type of the |
130
|
|
|
|
|
|
|
store, how to connect to it, and your current test user's session id. This is relatively trivial |
131
|
|
|
|
|
|
|
if a database (e.g. with L<Test::DBIx::Class>), but gets more complicated when you're not mocking |
132
|
|
|
|
|
|
|
the store and it's something a little more esoteric. Of course you could use a different store for |
133
|
|
|
|
|
|
|
your unit tests, but maybe you don't want to do that. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Enter Test::WWW::Mechanize::Catalyst::WithContext. Just grab the context before and after you |
136
|
|
|
|
|
|
|
perform your action and look at the sessions. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
# we don't need the response object for this request |
139
|
|
|
|
|
|
|
(undef, my $c_before) = $mech->get_context('/'); # or some other url |
140
|
|
|
|
|
|
|
my ( $res, $c_after ) = $mech->get_context('/change/session'); |
141
|
|
|
|
|
|
|
isnt $c_before->session->{foo}, $c_after->session->{foo}, 'foo got changed'; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Of course this could be arbitrarily complex. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 BUGS |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
If you find any bugs please L<open an issue on github|https://github.com/simbabque/Test-WWW-Mechanize-Catalyst-WithContext/issues>. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 SEE ALSO |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=over |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=item |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
L<Test::WWW::Mechanize::Catalyst> |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=item |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
L<Test::WWW::Mechanize> |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=item |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
L<WWW::Mechanize> |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=item |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
L<Catalyst::Test> |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=item |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
L<Catalyst> |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=back |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head1 ATTRIBUTION |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
This module borrows parts of its test suite from L<Test::WWW::Mechanize::Catalyst>. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head1 AUTHOR |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
simbabque <simbabque@cpan.org> |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head1 LICENSE |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Copyright (C) simbabque. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
188
|
|
|
|
|
|
|
it under the same terms as Perl itself. |