File: formatter.t

package info (click to toggle)
liboauth-lite2-perl 0.11-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 592 kB
  • sloc: perl: 2,658; makefile: 8
file content (103 lines) | stat: -rw-r--r-- 3,682 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
use strict;
use warnings;

use Test::More;

use OAuth::Lite2::Formatters;
use Try::Tiny;

my ($json, $xml, $form, $unknown);

TEST_FACTORY: {

    $json = OAuth::Lite2::Formatters->get_formatter_by_name("json");
    isa_ok($json, "OAuth::Lite2::Formatter::JSON");
    $json = OAuth::Lite2::Formatters->get_formatter_by_type("application/json");
    isa_ok($json, "OAuth::Lite2::Formatter::JSON");
    $json = OAuth::Lite2::Formatters->get_formatter_by_type("application/json; charset=utf-8");
    isa_ok($json, "OAuth::Lite2::Formatter::JSON");

    $xml = OAuth::Lite2::Formatters->get_formatter_by_name("xml");
    isa_ok($xml, "OAuth::Lite2::Formatter::XML");
    $xml = OAuth::Lite2::Formatters->get_formatter_by_type("application/xml");
    isa_ok($xml, "OAuth::Lite2::Formatter::XML");

    $form = OAuth::Lite2::Formatters->get_formatter_by_name("form");
    isa_ok($form, "OAuth::Lite2::Formatter::FormURLEncoded");
    $form = OAuth::Lite2::Formatters->get_formatter_by_type("application/x-www-form-urlencoded");
    isa_ok($form, "OAuth::Lite2::Formatter::FormURLEncoded");

    $unknown = OAuth::Lite2::Formatters->get_formatter_by_name("unknown");
    ok(!$unknown);
    $unknown = OAuth::Lite2::Formatters->get_formatter_by_type("unknown");
    ok(!$unknown);
};

my $params1 = {
    access_token        => q{foo},
    refresh_token       => q{bar},
    access_token_secret => q{buz},
    expires_in          => 3600,
};

TEST_JSON: {
    is($json->name, "json");
    is($json->type, "application/json");
    #is($json->format($params1), '{"expires_in":3600,"refresh_token":"bar","access_token_secret":"buz","access_token":"foo"}');

    my $parsed = $json->parse('{"expires_in":3600,"refresh_token":"bar","access_token_secret":"buz","access_token":"foo"}');

    is($parsed->{access_token}, q{foo});
    is($parsed->{refresh_token}, q{bar});
    is($parsed->{access_token_secret}, q{buz});
    is($parsed->{expires_in}, 3600);

    my $message;
    try {
        $json->parse("invalid format");
    } catch {
        $message = $_;
    };
    like($message, qr/malformed JSON string/);
};

TEST_XML: {
    is($xml->name, "xml");
    is($xml->type, "application/xml");
    like($xml->format($params1), qr/\A<\?xml\sversion=\"1\.0\"\sencoding=\"UTF-8\"\?><OAuth>.+<\/OAuth>\z/);
    like($xml->format($params1), qr/<expires_in>3600<\/expires_in>/);
    like($xml->format($params1), qr/<refresh_token>bar<\/refresh_token>/);
    like($xml->format($params1), qr/<access_token_secret>buz<\/access_token_secret>/);
    like($xml->format($params1), qr/<access_token>foo<\/access_token>/);

    my $parsed = $xml->parse('<?xml version="1.0" encoding="UTF-8"?><OAuth><expires_in>3600</expires_in><refresh_token>bar</refresh_token><access_token_secret>buz</access_token_secret><access_token>foo</access_token></OAuth>');

    is($parsed->{access_token}, q{foo});
    is($parsed->{refresh_token}, q{bar});
    is($parsed->{access_token_secret}, q{buz});
    is($parsed->{expires_in}, 3600);

    my $message;
    try {
        $xml->parse("invalid format");
    } catch {
        $message = $_;
    };
    like($message, qr/parser error/);
};

TEST_FORM: {
    is($form->name, "form");
    is($form->type, "application/x-www-form-urlencoded");
    is($form->format($params1), 'access_token=foo&access_token_secret=buz&expires_in=3600&refresh_token=bar');

    my $parsed = $form->parse('access_token=foo&access_token_secret=buz&expires_in=3600&refresh_token=bar');

    is($parsed->{access_token}, q{foo});
    is($parsed->{refresh_token}, q{bar});
    is($parsed->{access_token_secret}, q{buz});
    is($parsed->{expires_in}, 3600);
};

# TODO invalid format test
done_testing;