|
| 1 | +describe("TestTradeQuery", function() |
| 2 | + before_each(function() |
| 3 | + -- Mock necessary global objects and functions to isolate the test |
| 4 | + _G.new = function(className, ...) |
| 5 | + if className == "TradeQueryRateLimiter" then |
| 6 | + return { |
| 7 | + GetPolicyName = function() return "mock_policy" end, |
| 8 | + NextRequestTime = function() return 0 end, |
| 9 | + InsertRequest = function() return 1 end, |
| 10 | + FinishRequest = function() end, |
| 11 | + UpdateFromHeader = function() end |
| 12 | + } |
| 13 | + end |
| 14 | + -- Fallback for other classes if needed |
| 15 | + return {} |
| 16 | + end |
| 17 | + _G.main = { |
| 18 | + POESESSID = "mock_poesessid_for_testing" |
| 19 | + } |
| 20 | + _G.dkjson = require("dkjson") |
| 21 | + _G.tradeQuery = nil -- Mock or setup as needed |
| 22 | + end) |
| 23 | + |
| 24 | + teardown(function() |
| 25 | + -- Clean up global mocks |
| 26 | + _G.new = nil |
| 27 | + _G.main = nil |
| 28 | + _G.dkjson = nil |
| 29 | + _G.tradeQuery = nil |
| 30 | + end) |
| 31 | + |
| 32 | + it("should construct the correct whisper URL", function() |
| 33 | + local requests = newClass("TradeQueryRequests")() |
| 34 | + assert.are.equals("https://www.pathofexile.com/api/trade2/whisper", requests.hostName .. "api/trade2/whisper") |
| 35 | + end) |
| 36 | + |
| 37 | + it("should format the whisper request body correctly with a space", function() |
| 38 | + local requests = newClass("TradeQueryRequests")() |
| 39 | + local testToken = "test_token_12345" |
| 40 | + requests:SendWhisper(testToken, "http://mock.referer", function() end) |
| 41 | + |
| 42 | + local whisperRequest = requests.requestQueue.whisper[1] |
| 43 | + assert.is_not_nil(whisperRequest) |
| 44 | + |
| 45 | + local expectedBody = '{"token": "' .. testToken .. '"}' |
| 46 | + assert.are.equals(expectedBody, whisperRequest.body) |
| 47 | + end) |
| 48 | + |
| 49 | + it("should include correct headers for the whisper request", function() |
| 50 | + local requests = newClass("TradeQueryRequests")() |
| 51 | + local testToken = "test_token_12345" |
| 52 | + local testReferer = "https://www.pathofexile.com/trade2/search/poe2/Test%20League/abcdef123" |
| 53 | + requests:SendWhisper(testToken, testReferer, function() end) |
| 54 | + |
| 55 | + local whisperRequest = requests.requestQueue.whisper[1] |
| 56 | + assert.is_not_nil(whisperRequest) |
| 57 | + assert.is_not_nil(whisperRequest.headers) |
| 58 | + |
| 59 | + assert.truthy(string.find(whisperRequest.headers, "Content-Type: application/json", 1, true)) |
| 60 | + assert.truthy(string.find(whisperRequest.headers, "User-Agent: Mozilla/5.0", 1, true)) |
| 61 | + assert.truthy(string.find(whisperRequest.headers, "X-Requested-With: XMLHttpRequest", 1, true)) |
| 62 | + assert.truthy(string.find(whisperRequest.headers, "Referer: " .. testReferer, 1, true)) |
| 63 | + end) |
| 64 | + |
| 65 | + it("should correctly build a trade search URL for the referer header", function() |
| 66 | + local requests = newClass("TradeQueryRequests")() |
| 67 | + local url = requests:buildUrl("https://www.pathofexile.com/trade2/search", "poe2", "Rise of the Abyssal", "kMLBg0KC5") |
| 68 | + local expectedUrl = "https://www.pathofexile.com/trade2/search/poe2/Rise+of+the+Abyssal/kMLBg0KC5" |
| 69 | + assert.are.equals(expectedUrl, url) |
| 70 | + end) |
| 71 | +end) |
0 commit comments