| 1 | package CatalystX::Test::MessageDriven; | ||||||||||
| 2 | 2 | ▶ | use Class::MOP; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | ▶ | use Sub::Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use HTTP::Request; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | $ENV{CATALYST_ENGINE} = 'Test::MessageDriven'; | |||||||||
| 8 | }; | ||||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | CatalystX::Test::MessageDriven - test message-driven Catalyst apps | ||||||||||
| 13 | |||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||
| 15 | |||||||||||
| 16 | Derived from Catalyst::Test, this module provides a way to run tests | ||||||||||
| 17 | against message-driven Catalyst applications - those with | ||||||||||
| 18 | Catalyst::Controller::MessageDriven-based controllers, and expect to | ||||||||||
| 19 | run with Catalyst::Engine::Stomp. | ||||||||||
| 20 | |||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||
| 22 | |||||||||||
| 23 | BEGIN { use_ok 'CatalystX::Test::MessageDriven', 'SomeApp' }; | ||||||||||
| 24 | |||||||||||
| 25 | my $req = '... some message text ...'; | ||||||||||
| 26 | my $queue = 'somequeue'; | ||||||||||
| 27 | my $res = request($queue, $req); | ||||||||||
| 28 | ok($res); | ||||||||||
| 29 | |||||||||||
| 30 | =head1 EXPORTS | ||||||||||
| 31 | |||||||||||
| 32 | =head2 request(queue, message) | ||||||||||
| 33 | |||||||||||
| 34 | This function accepts a queue and a message, and runs the request in | ||||||||||
| 35 | that context. Returns a response object. | ||||||||||
| 36 | |||||||||||
| 37 | =head1 TODO | ||||||||||
| 38 | |||||||||||
| 39 | Some test wrappers - successful / error message conditions? | ||||||||||
| 40 | |||||||||||
| 41 | =cut | ||||||||||
| 42 | |||||||||||
| 43 | 2 | ▶ | my $message_driven_request = sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 2 | my ($app, $path, $req_message) = @_; | |||||||||
| 45 | 2 | my $url = "message://localhost:61613/$path"; | |||||||||
| 46 | |||||||||||
| 47 | 2 | my $request = HTTP::Request->new( POST => $url ); | |||||||||
| 48 | 2 | $request->content($req_message); | |||||||||
| 49 | 2 | $request->content_length(length $req_message); | |||||||||
| 50 | 2 | $request->content_type('application/octet-stream'); | |||||||||
| 51 | |||||||||||
| 52 | 2 | my $response; | |||||||||
| 53 | 2 | $app->handle_request($request, \$response); | |||||||||
| 54 | |||||||||||
| 55 | 2 | return $response; | |||||||||
| 56 | 2 | }; | |||||||||
| 57 | |||||||||||
| 58 | 2 | ▶ | my $build_exports = sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.4
| |||||||||||
| 59 | 2 | my ($self, $meth, $args, $defaults) = @_; | |||||||||
| 60 | |||||||||||
| 61 | 2 | my $request; | |||||||||
| 62 | 2 | my $class = $args->{class}; | |||||||||
| 63 | |||||||||||
| 64 | 2 | ▶ | if (!$class) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 0 | ▶ | $request = sub { Catalyst::Exception->throw("Must specify a test app: use CatalystX::Test::MessageDriven 'TestApp'") }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 66 | } | ||||||||||
| 67 | else { | ||||||||||
| 68 | 2 | ▶ | unless (Class::MOP::is_class_loaded($class)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 2 | Class::MOP::load_class($class); | |||||||||
| 70 | } | ||||||||||
| 71 | 2 | $class->import; | |||||||||
| 72 | |||||||||||
| 73 | 2 | my $app = $class->run(); | |||||||||
| 74 | 2 | ▶ | $request = sub { $message_driven_request->( $app, @_ ) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | return { | ||||||||||
| 78 | 2 | request => $request, | |||||||||
| 79 | }; | ||||||||||
| 80 | 2 | }; | |||||||||
| 81 | |||||||||||
| 82 | { | ||||||||||
| 83 | 2 | my $import = Sub::Exporter::build_exporter({ | |||||||||
| 84 | groups => [ all => $build_exports ], | ||||||||||
| 85 | into_level => 1, | ||||||||||
| 86 | }); | ||||||||||
| 87 | |||||||||||
| 88 | 2 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||
| 89 | 2 | my ($self, $class) = @_; | |||||||||
| 90 | 2 | $import->($self, '-all' => { class => $class }); | |||||||||
| 91 | 2 | return 1; | |||||||||
| 92 | } | ||||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | package # Hide from PAUSE | ||||||||||
| 96 | 2 | Catalyst::Engine::Test::MessageDriven; | |||||||||
| 97 | 2 | ▶ | use base 'Catalyst::Engine::Embeddable'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 98 | |||||||||||
| 99 | 2 | ▶ | sub run { | ||||||||
|
Subroutine
run: called CC=1 SCAR=0.0
| |||||||||||
| 100 | 2 | my ($self, $app) = @_; | |||||||||
| 101 | 2 | return $app; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | 2 | 1; | |||||||||
| 105 | |||||||||||
| 106 | =head1 AUTHOR AND CONTRIBUTORS | ||||||||||
| 107 | |||||||||||
| 108 | See information in L<Catalyst::Engine::Stomp> | ||||||||||
| 109 | |||||||||||
| 110 | =head1 LICENCE AND COPYRIGHT | ||||||||||
| 111 | |||||||||||
| 112 | Copyright (C) 2009 Venda Ltd | ||||||||||
| 113 | |||||||||||
| 114 | This library is free software; you can redistribute it and/or modify | ||||||||||
| 115 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||||||||||
| 116 | at your option, any later version of Perl 5 you may have available. | ||||||||||
| 117 | |||||||||||
| 118 | =cut | ||||||||||
| 119 | |||||||||||