echo_lab_madgex_com.lua 1.89 KB
Newer Older
1 2 3 4 5 6 7 8
#!/usr/bin/env lua

--[[
        http://echo.lab.madgex.com is an OAuth Test Server
--]]

local Spore = require 'Spore'

9
local keys = {
10
    realm = '',
François Perrad's avatar
François Perrad committed
11 12
    oauth_consumer_key    = 'key',
    oauth_consumer_secret = 'secret',
13
}
14 15 16 17 18
local client = Spore.new_from_string [[
{
    "base_url" : "http://echo.lab.madgex.com",
    "name" : "madgex",
    "methods" : {
19 20 21 22 23 24 25 26 27 28
        "get_request_token" : {
            "path" : "/request-token.ashx",
            "method" : "GET",
            "expected_status" : [ 200, 400 ]
        },
        "get_access_token" : {
            "path" : "/access-token.ashx",
            "method" : "GET",
            "expected_status" : [ 200, 400 ]
        },
29 30 31 32
        "echo" : {
            "path" : "/echo.ashx",
            "method" : "GET",
            "unattended_params" : true,
33 34
            "expected_status" : [ 200, 400, 401 ]
        },
François Perrad's avatar
François Perrad committed
35 36 37 38 39
        "echo_p" : {
            "path" : "/echo.ashx",
            "method" : "POST",
            "unattended_params" : true,
            "expected_status" : [ 200, 400, 401 ]
40 41 42 43 44
        }
    },
    "authentication" : true
}
]]
45 46 47 48 49
client:enable('Auth.OAuth', keys)

local r = client:get_request_token()
assert(#r.body > 0, r.status)
print(r.body)
François Perrad's avatar
François Perrad committed
50 51 52
for k, v in r.body:gmatch'([^&=]+)=([^&=]*)&?' do keys[k] = v end
assert(keys.oauth_token == 'requestkey')
assert(keys.oauth_token_secret == 'requestsecret')
53

François Perrad's avatar
François Perrad committed
54
r = client:get_access_token()
55 56
assert(#r.body > 0, r.status)
print(r.body)
François Perrad's avatar
François Perrad committed
57 58 59
for k, v in r.body:gmatch'([^&=]+)=([^&=]*)&?' do keys[k] = v end
assert(keys.oauth_token == 'accesskey')
assert(keys.oauth_token_secret == 'accesssecret')
60

François Perrad's avatar
François Perrad committed
61
r = client:echo{ method = 'foo bar', bar = 'baz' }
62
print(r.body)
François Perrad's avatar
François Perrad committed
63
assert(r.body == 'method=foo bar&bar=baz')
64

François Perrad's avatar
François Perrad committed
65
r = client:echo_p{ method = 'foo bar', bar = 'baz' }
François Perrad's avatar
François Perrad committed
66
print(r.body)
François Perrad's avatar
François Perrad committed
67
assert(r.body == 'method=foo bar&bar=baz')
François Perrad's avatar
François Perrad committed
68

69
print 'ok - http://echo.lab.madgex.com'
François Perrad's avatar
François Perrad committed
70
print '1..1'