FastAPI/Response/Redirect.pm

filter: Statementstmt 100% 26 / 26 Branchbran 100% 2 / 2 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 8 / 8 Podpod 50.0% 1 / 2 totaltotal 93.4% 43 / 46 CC 5 SCAR 6.1
CC
5
Cov
97%
__ANON__
3.0
prepare_headers
2.0
redirect_to
2.0
CRAP 1.8
« FastAPI/RateLimit/Driver/Memory.pm ↑ Summary FastAPI/Response.pm »
1 package PAGI::FastAPI::Response::Redirect;
2  
3 3 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use experimental 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = qv('v1.7.3');
8 3 our $AUTHORITY = 'cpan:MANWAR';
9  
10 3 ▶use PAGI::FastAPI::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our @EXPORT_OK = qw(redirect_to);
14  
15 # Built on PAGI::FastAPI::Response's existing, documented public contract
16 # (status/body/headers + prepare_headers($c, $default_content_type)), the
17 # same way PAGI::FastAPI::Response::HTML and ::SSE are built, the dispatcher
18 # accepts any class that isa('PAGI::FastAPI::Response'), by design.
19  
20 3 class PAGI::FastAPI::Response::Redirect :isa(PAGI::FastAPI::Response) {
21 3 field $location :param;
22  
23 # 'field $location :param;' with no default already makes Perl's own
24 # generated constructor require the argument to be PASSED, that
25 # check fires before this ADJUST block runs, and produces its own
26 # "Required parameter 'location' is missing" message, not this one.
27 # This ADJUST only actually fires for the one case Perl's own check
28 # doesn't cover: an explicitly-passed EMPTY STRING, which satisfies
29 # "a value was passed" but isn't a usable location. Verified by
30 # actually running both cases, see t/26-response_redirect.t.
31 6 ▶ ADJUST {
Subroutine __ANON__: called CC=3 SCAR=11.1
32 6 ▶ die "PAGI::FastAPI::Response::Redirect requires a 'location' argument"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $location and length $location 1 5 6
Condition66%2/3
defined $location and length $location
!ll&&!rl&&r
015
MC/DC50%1/2
defined $location and length $location
defined $location length $location
Truth table66%2/3
A and B
A= defined $locationB= length $location
ABresult
0X0
100
111
33 unless defined $location && length $location;
34 }
35  
36 3 ▶ method prepare_headers ($c, $default_content_type = 'text/plain; charset=utf-8') {
Subroutine prepare_headers: called CC=2 SCAR=6.9
Pod undocumented
37 3 $self->SUPER::prepare_headers($c, $default_content_type);
38 3 $c->set_header('location' => $location);
39 3 }
40 }
41  
42 # A plain sub with a signature must come AFTER a 'class ... :isa(...)' block
43 # in the same file, placing it before confuses the parser under
44 # "use experimental 'class'" in this Perl version (a 'class Name :isa(...)'
45 # declaration gets misread as if it were an out-of-order subroutine
46 # attribute). Order matters here, not just style.
47 #
48 # PAGI::FastAPI::Response's 'status' field is private with no setter, so a
49 # subclass cannot change its inherited default of 200, only the caller of
50 # ->new can, by passing status explicitly. This helper exists purely so
51 # callers don't have to remember "redirects need status => 302" every time.
52 4 ▶sub redirect_to ($location, %opts) {
Subroutine redirect_to: called CC=2 SCAR=6.9
Pod documented
53 return PAGI::FastAPI::Response::Redirect->new(
54 location => $location,
55 4 ▶ status => $opts{status} // 302,
Condition100%2/2
$opts{"status"} // 302
l!l
13
MC/DC100%1/1
$opts{"status"} // 302
$opts{"status"}
Truth table100%2/2
A
A= $opts{"status"}
Aresult
11
00
56 %opts,
57 );
58 }
59  
60 =encoding utf-8
61  
62 =head1 NAME
63  
64 PAGI::FastAPI::Response::Redirect - HTTP Redirect Response for PAGI::FastAPI
65  
66 =head1 VERSION
67  
68 Version v1.7.3
69  
70 =head1 SYNOPSIS
71  
72 use PAGI::FastAPI::Response::Redirect qw(redirect_to);
73  
74 $app->get('/old-path',
75 handler => async sub ($c) {
76 return redirect_to('/new-path'); # defaults to 302
77 }
78 );
79  
80 $app->get('/permanent-move',
81 handler => async sub ($c) {
82 return redirect_to('/new-path', status => 301);
83 }
84 );
85  
86 # Or construct directly:
87 use PAGI::FastAPI::Response::Redirect;
88  
89 $app->get('/old-path',
90 handler => async sub ($c) {
91 return PAGI::FastAPI::Response::Redirect->new(
92 location => '/new-path',
93 status => 307,
94 );
95 }
96 );
97  
98 =head1 DESCRIPTION
99  
100 A thin subclass of L<PAGI::FastAPI::Response> that sets the C<Location>
101 header and an appropriate redirect status code (defaulting to C<302 Found>
102 via the base class's C<status> default, pass C<status> explicitly for
103 C<301>, C<303>, C<307>, or C<308> as your use case requires).
104  
105 =head1 METHODS
106  
107 =head2 C<redirect_to($location, %opts)>
108  
109 Exported on request. Convenience function equivalent to
110 C<< PAGI::FastAPI::Response::Redirect->new(location => $location, status => 302, %opts) >>,
111 so you don't have to remember to pass C<status> for the common case.
112 Pass C<status> in C<%opts> to override (e.g. C<301>, C<303>, C<307>, C<308>).
113  
114 =head2 C<new(%options)>
115  
116 =over 4
117  
118 =item * C<location> - (Required) The target URL or path for the C<Location> header.
119  
120 =item * C<status> - (Optional) HTTP status code. Inherited from
121 L<PAGI::FastAPI::Response>, whose own default is C<200>, for an actual
122 redirect, pass C<status> explicitly (or use C<redirect_to()> above, which
123 defaults it to C<302> for you).
124  
125 =item * C<body> - (Optional) Response body. Defaults to C<''> (empty), which
126 is correct for most redirects.
127  
128 =back
129  
130 =head1 SEE ALSO
131  
132 L<PAGI::FastAPI::Response>, L<PAGI::FastAPI::Response::HTML>, L<PAGI::FastAPI::Response::SSE>
133  
134 =head1 AUTHOR
135  
136 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
137  
138 =head1 REPOSITORY
139  
140 L<https://github.com/manwar/PAGI-FastAPI>
141  
142 =head1 BUGS
143  
144 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/PAGI-FastAPI/issues>.
145 I will be notified and then you'll automatically be notified of progress on your
146 bug as I make changes.
147  
148 =head1 SUPPORT
149  
150 You can find documentation for this module with the perldoc command.
151  
152 perldoc PAGI::FastAPI::Response::Redirect
153  
154 You can also look for information at:
155  
156 =over 4
157  
158 =item * BUG Report
159  
160 L<https://github.com/manwar/PAGI-FastAPI/issues>
161  
162 =item * Search MetaCPAN
163  
164 L<https://metacpan.org/dist/PAGI-FastAPI/>
165  
166 =back
167  
168 =head1 LICENSE AND COPYRIGHT
169  
170 Copyright (C) 2026 Mohammad Sajid Anwar.
171  
172 This program is free software; you can redistribute it and/or modify it under
173 the terms of the Artistic License (2.0).
174  
175 =cut
176  
177 3 1; # End of PAGI::FastAPI::Response::Redirect
« FastAPI/RateLimit/Driver/Memory.pm ↑ Summary FastAPI/Response.pm »