Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send User-Agent with platform and system information #214

Merged
merged 5 commits into from
Jan 11, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[user-agent] introduce user agent module
RFC 7231 complainant User-Agent
mikz committed Jan 11, 2017

Verified

This commit was signed with the committer’s verified signature.
mikz Michal Cichra
commit c8923ac9b7b4f99ec246811062492fa2209df041
75 changes: 75 additions & 0 deletions apicast/src/user_agent.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
local ffi = require 'ffi'
local module = require 'module'

local setmetatable = setmetatable

local _M = {
_VERSION = '2.0'
}

function _M.deployment()
return _M.threescale_deployment_env or 'unknown'
end

-- User-Agent: <product> / <product-version> <comment>
-- User-Agent: Mozilla/<version> (<system-information>) <platform> (<platform-details>) <extensions>

function _M.call()
return 'APIcast/' .. _M._VERSION .. ' (' .. _M.system_information() .. ') ' .. _M.platform()
end

function _M.system_information()
return ffi.os .. '; ' .. ffi.arch .. '; env:' .. _M.deployment()
end

function _M.platform()
local m = module.new()
local table, err = m:require()
local version = table and table._VERSION
local name = table and table._NAME or m.name

if not name then
return nil, 'missing module name'
end

if not table and err then
return nil, err
end

if version then
return name ..'/'.. version
else
return name
end
end

local mt = {
__call = _M.call,
__tostring = _M.call
}

function _M.reset()
local env = {
threescale_deployment_env = os.getenv('THREESCALE_DEPLOYMENT_ENV')
}

mt.__index = env

_M.env = env

mt.__call = _M.call
mt.__tostring = _M.call
end

function _M.cache()
_M.reset()

local user_agent = _M.call()

mt.__call = function() return user_agent end
mt.__tostring = mt.__call
end

setmetatable(_M, mt)

return _M
71 changes: 71 additions & 0 deletions spec/user_agent_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
local user_agent = require 'user_agent'
local ffi = require("ffi")

describe('3scale', function()
before_each(function() user_agent.reset() end)

describe('.deployment', function()
it('reads from environment', function()
stub(os, 'getenv').on_call_with('THREESCALE_DEPLOYMENT_ENV').returns('foobar')

user_agent.reset()

assert.same('foobar', user_agent.deployment())
end)

it('uses internal structure', function()
user_agent.env.threescale_deployment_env = 'bar'

assert.same('bar', user_agent.deployment())
end)
end)

describe('.user_agent', function()
-- User-Agent: <product> / <product-version> <comment>
-- User-Agent: Mozilla/<version> (<system-information>) <platform> (<platform-details>) <extensions>

it('matches common format', function()

user_agent.env.threescale_deployment_env = 'production'

assert.match('APIcast/' .. user_agent._VERSION, user_agent.call())
end)

it('includes system information', function()
assert.match('(' .. user_agent.system_information() .. ')', user_agent.call())
end)

it('includes platform information', function()
assert.match(' ' .. user_agent.platform(), user_agent.call())
end)

it('works as tostring', function()
assert.equal(user_agent.call(), tostring(user_agent))
end)

it('works as function', function()
assert.equal(user_agent.call(), user_agent())
end)
end)


describe('.system_information', function()
it('includes os information', function()
assert.match(ffi.os ..'; ' .. ffi.arch, user_agent.system_information())
end)

it('includes deployment information', function()
user_agent.env.threescale_deployment_env = 'foobar'
assert.match(' env:foobar', user_agent.system_information())
end)
end)

describe('.platform', function()
it('includes os information', function()
local apicast = require('apicast')

assert.same('APIcast/' .. apicast._VERSION, user_agent.platform())
end)
end)

end)