File: base.t

package info (click to toggle)
libtwitter-api-perl 1.0006-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 424 kB
  • sloc: perl: 2,868; makefile: 7
file content (226 lines) | stat: -rw-r--r-- 6,353 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!perl
use strict;
use warnings;

use HTTP::Response;
use Test::Fatal;
use Test::Spec;
use URL::Encode qw/url_params_mixed/;

use Twitter::API;

sub http_response_ok {
    HTTP::Response->new(
        200, 'OK',
        [
            content_type   => 'application/json;charset=utf-8',
            contest_length => 4,
        ],
        '{}'
    );
}

describe 'construction' => sub {

    it 'requires consumer_key' => sub {
       like(
            exception { Twitter::API->new },
            qr/Missing .* consumer_key/,
        );
    };

    it 'requires consumer_secret' => sub {
        like(
            exception { Twitter::API->new(consumer_key => 'key') },
            qr/Missing .* consumer_secret/,
        );
    };

    it 'instantiates a minimal object' => sub {
        exception {
            Twitter::API->new(
                consumer_key    => 'key',
                consumer_secret => 'secret',
            );
        }, undef;
    };
};

describe 'request' => sub {
    my $client;
    before each => sub {
        $client = Twitter::API->new(
            consumer_key    => 'key',
            consumer_secret => 'secret',
        );

        $client->stubs(send_request => \&http_response_ok);
    };

    it 'requires an access_token' => sub {
        like(
            exception { $client->request(get => 'fake/endpoint') },
            qr/token/,
        );
    };

    it 'accepts per request tokens' => sub {
        exception {
            $client->get('fake/endpoint', {
                -token        => 'my-token',
                -token_secret => 'my-secret',
            });
        }, undef;

    };

    it "uses object's user credentials" => sub {
        exception {
            $client->access_token('token');
            $client->access_token_secret('token-secret');
            $client->get('fake/endpoint');
        }, undef;
    };

    it 'prioritizes per-request user credentials' => sub {
        $client->access_token('token');
        $client->access_token_secret('token-secret');
        my ($r, $c) = $client->get('fake/endpoint', {
            -token        => 'per-request',
            -token_secret => 'per-request-secret',
        });
        my $req = $c->http_request;
        like $req->header('authorization'), qr/oauth_token="per-request"/;
    };
};

describe 'get' => sub {
    my $req;
    before each => sub {
        my $client = Twitter::API->new(
            consumer_key        => 'key',
            consumer_secret     => 'secret',
        );
        $client->stubs(send_request => \&http_response_ok);
        my ($r, $c) = $client->get('fake/endpoint', {
            -token        => 'token',
            -token_secret => 'access_token_secret',
            foo           => 'bar',
            baz           => 'bop',
        });
        $req = $c->http_request;
    };

    it 'uses method GET' => sub { is $req->method, 'GET' };
    it 'expands url' => sub {
        like $req->uri, qr{^\Qhttps://api.twitter.com/1.1/fake/endpoint.json};
    };
    it 'passes API arguments' => sub {
        is_deeply { $req->uri->query_form }, { foo => 'bar', baz => 'bop' };
    };
    it 'creates valid authorization header' => sub {
        like $req->header('authorization'), qr/
            OAuth\ oauth_consumer_key="key",\s*
            oauth_nonce="[^"]+",\s*
            oauth_signature="[^"]+",\s*
            oauth_signature_method="HMAC-SHA1",\s*
            oauth_timestamp="\d+",\s*
            oauth_token="token",\s*
            oauth_version="1\.0"
        /x;
    };

};

describe 'post' => sub {
    my $req;
    before each => sub {
        my $client = Twitter::API->new(
            consumer_key        => 'key',
            consumer_secret     => 'secret',
            access_token        => 'token',
            access_token_secret => 'secret',
        );
        $client->stubs(send_request => \&http_response_ok);
        my ($r, $c) = $client->post('fake/endpoint', {
            foo => 'bar',
            baz => 'bop',
        });
        $req = $c->http_request;
    };

    it 'has method POST' => sub { is $req->method, 'POST' };
    it 'has uses correct Contect-Type' => sub {
        is $req->content_type, 'application/x-www-form-urlencoded';
    };
    it 'passes API arguments' => sub {
        is_deeply url_params_mixed($req->decoded_content), {
            foo => 'bar',
            baz => 'bop',
        };
    };
};

describe 'post (file upload)' => sub {
    my $req;
    before each => sub {
        my $client = Twitter::API->new(
            consumer_key        => 'key',
            consumer_secret     => 'secret',
            access_token        => 'token',
            access_token_secret => 'secret',
        );
        $client->stubs(send_request => \&http_response_ok);
        my ($r, $c) = $client->post('fake/endpoint', {
            foo  => 'bar',
            baz  => 'bop',
            file => [ undef, 'file', content => 'just some text' ],
        });
        $req = $c->http_request;
    };

    it 'has correct content type' => sub {
        is $req->content_type, 'multipart/form-data';
    };
    it 'passes API args' => sub {
        my %args;
        for ( $req->parts ) {
            my ( $name ) = $_->header('content_disposition') =~ / name="([^"]+)"/;
            my $value = $_->decoded_content;
            $args{$name} = $value;
        }
        is_deeply \%args, {
            foo  => 'bar',
            baz  => 'bop',
            file => 'just some text',
        };
    };
};

describe 'post (json body)' => sub {
    my ( $client, $req );
    before each => sub {
        $client = Twitter::API->new(
            consumer_key        => 'key',
            consumer_secret     => 'secret',
            access_token        => 'token',
            access_token_secret => 'secret',
        );
        $client->stubs(send_request => \&http_response_ok);
        my ($r, $c) = $client->post('fake/endpoint', {
            -to_json => { foo => 'bar', baz => 'bop' },
        });
        $req = $c->http_request;
    };

    it 'has correct content type' => sub {
        is $req->content_type, 'application/json';
    };
    it 'has carrect content' => sub {
        my $json = $req->decoded_content;
        my $data = $client->from_json($json);
        is_deeply $data, { foo => 'bar', baz => 'bop' };
    };
};

runtests;