Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions assets/js/phoenix/longpoll.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ export default class LongPoll {
this.ajax("GET", headers, null, () => this.ontimeout(), resp => {
if(resp){
var {status, token, messages} = resp
if(status === 410 && this.token !== null){
// In case we already have a token, this means that our existing session
// is gone. We fail so that the client rejoins its channels.
this.onerror(410)
this.closeAndRetry(3410, "session_gone", false)
return
}
this.token = token
} else {
status = 0
Expand Down
27 changes: 24 additions & 3 deletions assets/test/longpoll_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ describe("LongPoll", () => {
const authToken = "my-auth-token"
const encodedToken = btoa(authToken)
const protocols = ["phoenix", `${AUTH_TOKEN_PREFIX}${encodedToken}`]

const longpoll = new LongPoll("http://localhost/socket/longpoll", protocols)
longpoll.timeout = 1000
longpoll.poll()

// Verify Ajax.request was called with the correct headers
expect(Ajax.request).toHaveBeenCalledWith(
"GET",
Expand All @@ -104,7 +104,7 @@ describe("LongPoll", () => {
const longpoll = new LongPoll("http://localhost/socket/longpoll", undefined)
longpoll.timeout = 1000
longpoll.poll()

// Verify Ajax.request was called without auth token header
expect(Ajax.request).toHaveBeenCalledWith(
"GET",
Expand All @@ -116,6 +116,27 @@ describe("LongPoll", () => {
expect.any(Function)
)
})

it("should treat 410 as error when token already exists", () => {
const longpoll = new LongPoll("http://localhost/socket/longpoll", undefined)
longpoll.timeout = 1000
longpoll.token = "existing-token"

const mockOnerror = jest.fn()
const mockCloseAndRetry = jest.fn()
longpoll.onerror = mockOnerror
longpoll.closeAndRetry = mockCloseAndRetry

Ajax.request.mockImplementation((method, url, headers, body, timeout, ontimeout, callback) => {
callback({status: 410, token: "new-token", messages: []})
return {abort: jest.fn()}
})

longpoll.poll()

expect(mockOnerror).toHaveBeenCalledWith(410)
expect(mockCloseAndRetry).toHaveBeenCalledWith(3410, "session_gone", false)
})
})

describe("batchSend", () => {
Expand Down