Refactor to make the maintanance simpler
- Fixed issue with webhooks
This commit is contained in:
+1
-1
@@ -1,2 +1,2 @@
|
||||
client: go run client.go
|
||||
server: go run ../main.go -config ./functional-config.json -alsologtostderr
|
||||
server: go run ../cmd/main.go -config ./functional-config.json -alsologtostderr
|
||||
+30
-9
@@ -5,6 +5,7 @@ import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
|
||||
"github.com/pusher/pusher-http-go"
|
||||
)
|
||||
@@ -13,10 +14,11 @@ var client pusher.Client
|
||||
|
||||
func init() {
|
||||
client = pusher.Client{
|
||||
AppId: "1",
|
||||
Key: "278d525bdf162c739803",
|
||||
Secret: "7ad3753142a6693b25b9",
|
||||
Host: ":8080",
|
||||
AppId: "1",
|
||||
Key: "278d525bdf162c739803",
|
||||
Secret: "7ad3753142a6693b25b9",
|
||||
Host: ":8080",
|
||||
Cluster: "hello",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +36,7 @@ func pusherPresenceAuth(res http.ResponseWriter, req *http.Request) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Fprint(res, string(response))
|
||||
_, _ = fmt.Fprint(res, string(response))
|
||||
}
|
||||
|
||||
func pusherPrivateAuth(res http.ResponseWriter, req *http.Request) {
|
||||
@@ -48,19 +50,38 @@ func pusherPrivateAuth(res http.ResponseWriter, req *http.Request) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Fprint(res, string(response))
|
||||
_, _ = fmt.Fprint(res, string(response))
|
||||
}
|
||||
|
||||
func triggerMessage(res http.ResponseWriter, _ *http.Request) {
|
||||
client.Trigger("private-messages", "messages", "The message from server")
|
||||
_, err := client.Trigger("private-messages", "messages", "The message from server")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Fprint(res, "OK")
|
||||
_, _ = fmt.Fprint(res, "OK")
|
||||
}
|
||||
|
||||
func hookcallback(res http.ResponseWriter, r *http.Request) {
|
||||
bytes, err := httputil.DumpRequest(r, true)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(string(bytes))
|
||||
|
||||
_, err = client.Trigger("private-webhook", "mywebhook", "The Webhoook from server")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
_, _ = fmt.Fprint(res, "OK")
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/pusher/presence/auth", pusherPresenceAuth)
|
||||
http.HandleFunc("/pusher/private/auth", pusherPrivateAuth)
|
||||
http.HandleFunc("/trigger", triggerMessage)
|
||||
http.HandleFunc("/hook", hookcallback)
|
||||
http.Handle("/", http.FileServer(http.Dir("./")))
|
||||
http.ListenAndServe(":5000", nil)
|
||||
_ = http.ListenAndServe(":5000", nil)
|
||||
}
|
||||
|
||||
@@ -14,8 +14,8 @@
|
||||
"Name": "App for Functional Test",
|
||||
"AppID": "1",
|
||||
"UserEvents": true,
|
||||
"WebHooks": false,
|
||||
"URLWebHook": "http://127.0.0.1:4567/php/hook.php"
|
||||
"WebHooks": true,
|
||||
"URLWebHook": "http://127.0.0.1:5000/hook"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
+127
-106
@@ -1,136 +1,157 @@
|
||||
let assert = chai.assert;
|
||||
|
||||
var assert = chai.assert;
|
||||
let APP_KEY = "278d525bdf162c739803";
|
||||
let HOST = "localhost";
|
||||
let PORT = 8080;
|
||||
let AUTH = "http://localhost:5000/pusher/private/auth";
|
||||
let AUTH_PRESENCE = "http://localhost:5000/pusher/presence/auth";
|
||||
|
||||
var APP_KEY = "278d525bdf162c739803";
|
||||
var HOST = "localhost";
|
||||
var PORT = 8080;
|
||||
var AUTH = "http://localhost:5000/pusher/private/auth"
|
||||
var AUTH_PRESENCE = "http://localhost:5000/pusher/presence/auth"
|
||||
|
||||
Pusher.log = function(msg) {
|
||||
console.log(msg);
|
||||
Pusher.log = function (msg) {
|
||||
console.log(msg);
|
||||
};
|
||||
|
||||
function getPusher(auth) {
|
||||
return new Pusher(APP_KEY, {
|
||||
wsHost: HOST,
|
||||
wsPort: PORT,
|
||||
authEndpoint: auth,
|
||||
enabledTransports: ["ws"],
|
||||
disabledTransports: ["flash"],
|
||||
cluster: "hello", // Should be ignored
|
||||
});
|
||||
return new Pusher(APP_KEY, {
|
||||
wsHost: HOST,
|
||||
wsPort: PORT,
|
||||
authEndpoint: auth,
|
||||
enabledTransports: ["ws"],
|
||||
disabledTransports: ["flash"],
|
||||
cluster: "hello", // Should be ignored
|
||||
});
|
||||
}
|
||||
|
||||
describe("Pusher", function() {
|
||||
describe("Pusher", function () {
|
||||
|
||||
describe("connection", function() {
|
||||
it("should connect sucessfully with correct config", function(done) {
|
||||
var pusher = getPusher(AUTH);
|
||||
describe("connection", function () {
|
||||
it("should connect sucessfully with correct config", function (done) {
|
||||
let pusher = getPusher(AUTH);
|
||||
|
||||
pusher.connection.bind('connected', function() {
|
||||
assert.ok(true, "Connected");
|
||||
done();
|
||||
});
|
||||
});
|
||||
pusher.connection.bind('connected', function () {
|
||||
assert.ok(true, "Connected");
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it("should not connect without the correct config", function(done) {
|
||||
var pusher = new Pusher("INVALID_APP_KEY", {
|
||||
wsHost: HOST,
|
||||
wsPort: PORT,
|
||||
enabledTransports: ["ws"],
|
||||
disabledTransports: ["flash"]
|
||||
});
|
||||
it("should not connect without the correct config", function (done) {
|
||||
let pusher = new Pusher("INVALID_APP_KEY", {
|
||||
wsHost: HOST,
|
||||
wsPort: PORT,
|
||||
enabledTransports: ["ws"],
|
||||
disabledTransports: ["flash"]
|
||||
});
|
||||
|
||||
pusher.connection.bind('disconnected', function() {
|
||||
assert.ok(true, "Not Connected");
|
||||
done();
|
||||
});
|
||||
});
|
||||
}); // connection
|
||||
pusher.connection.bind('disconnected', function () {
|
||||
assert.ok(true, "Not Connected");
|
||||
done();
|
||||
});
|
||||
});
|
||||
}); // connection
|
||||
|
||||
describe("subscription", function() {
|
||||
it("should subscribe to a public channel", function(done) {
|
||||
var pusher = getPusher(AUTH);
|
||||
describe("subscription", function () {
|
||||
it("should subscribe to a public channel", function (done) {
|
||||
let pusher = getPusher(AUTH);
|
||||
|
||||
var channel = pusher.subscribe('public-channel');
|
||||
channel.bind("pusher:subscription_succeeded", function(data) {
|
||||
assert.ok(true, "Connected to the channel");
|
||||
done();
|
||||
});
|
||||
});
|
||||
let channel = pusher.subscribe('public-channel');
|
||||
channel.bind("pusher:subscription_succeeded", function (data) {
|
||||
assert.ok(true, "Connected to the channel");
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it("should subscribe to a private channel", function(done) {
|
||||
var pusher = getPusher(AUTH);
|
||||
it("should subscribe to a private channel", function (done) {
|
||||
let pusher = getPusher(AUTH);
|
||||
|
||||
var channel = pusher.subscribe('private-channel');
|
||||
channel.bind("pusher:subscription_succeeded", function(data) {
|
||||
assert.ok(true, "Connected to the channel");
|
||||
done();
|
||||
});
|
||||
});
|
||||
let channel = pusher.subscribe('private-channel');
|
||||
channel.bind("pusher:subscription_succeeded", function (data) {
|
||||
assert.ok(true, "Connected to the channel");
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it("should subscribe to a presence channel", function(done) {
|
||||
var pusher = getPusher(AUTH_PRESENCE);
|
||||
it("should subscribe to a presence channel", function (done) {
|
||||
let pusher = getPusher(AUTH_PRESENCE);
|
||||
|
||||
var channel = pusher.subscribe('presence-channel');
|
||||
channel.bind("pusher:subscription_succeeded", function(data) {
|
||||
assert.ok(true, "Connected to the channel");
|
||||
done();
|
||||
});
|
||||
});
|
||||
}); // subscription
|
||||
let channel = pusher.subscribe('presence-channel');
|
||||
channel.bind("pusher:subscription_succeeded", function (data) {
|
||||
assert.ok(true, "Connected to the channel");
|
||||
done();
|
||||
});
|
||||
});
|
||||
}); // subscription
|
||||
|
||||
describe("events", function() {
|
||||
it('should not allowed client events on public channels', function(done) {
|
||||
var pusher = getPusher(AUTH);
|
||||
var channel = pusher.subscribe('public-channel');
|
||||
describe("hooks", function () {
|
||||
it('should receive hook', function (done) {
|
||||
let pusher = getPusher(AUTH);
|
||||
let channel = pusher.subscribe('private-webhook');
|
||||
|
||||
channel.bind("pusher:subscription_succeeded", function(data) {
|
||||
channel.trigger("client-message", "The message");
|
||||
});
|
||||
channel.bind("pusher:subscription_succeeded", function (data) {
|
||||
console.log("subscribed");
|
||||
});
|
||||
|
||||
pusher.bind("pusher:error", function(data) {
|
||||
assert.ok(true, "Expected error");
|
||||
done();
|
||||
});
|
||||
});
|
||||
channel.bind("mywebhook", function (data) {
|
||||
console.log(data);
|
||||
assert.equal(data, "The Webhoook from server");
|
||||
done();
|
||||
});
|
||||
});
|
||||
}); // hooks
|
||||
|
||||
it('should allow client events on private channels', function(done) {
|
||||
var pusher_a = getPusher(AUTH);
|
||||
var pusher_b = getPusher(AUTH);
|
||||
describe("events", function () {
|
||||
it('should not allowed client events on public channels', function (done) {
|
||||
let pusher = getPusher(AUTH);
|
||||
let channel = pusher.subscribe('public-channel');
|
||||
|
||||
var channel_a = pusher_a.subscribe('private-channel');
|
||||
var channel_b = pusher_b.subscribe('private-channel');
|
||||
channel.bind("pusher:subscription_succeeded", function (data) {
|
||||
channel.trigger("client-message", "The message");
|
||||
});
|
||||
|
||||
channel_a.bind("pusher:subscription_succeeded", function() {
|
||||
channel_a.trigger("client-message", "The message");
|
||||
});
|
||||
pusher.bind("pusher:error", function (data) {
|
||||
assert.ok(true, "Expected error");
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
channel_b.bind("client-message", function(data) {
|
||||
assert.equal(data, "The message");
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should allow client events on private channels', function (done) {
|
||||
let pusher_a = getPusher(AUTH);
|
||||
let pusher_b = getPusher(AUTH);
|
||||
|
||||
it('should publish event on private channel', function(done) {
|
||||
var pusher_a = getPusher(AUTH);
|
||||
var pusher_b = getPusher(AUTH);
|
||||
let channel_a = pusher_a.subscribe('private-channel');
|
||||
let channel_b = pusher_b.subscribe('private-channel');
|
||||
|
||||
var channel_a = pusher_a.subscribe('private-messages');
|
||||
var channel_b = pusher_b.subscribe('private-messages');
|
||||
channel_a.bind("pusher:subscription_succeeded", function () {
|
||||
channel_a.trigger("client-message", "The message");
|
||||
});
|
||||
|
||||
channel_a.bind("pusher:subscription_succeeded", function() {
|
||||
var xhttp = new XMLHttpRequest();
|
||||
xhttp.open("GET", "/trigger", true);
|
||||
xhttp.send();
|
||||
});
|
||||
channel_b.bind("client-message", function (data) {
|
||||
assert.equal(data, "The message");
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
channel_b.bind("messages", function(data) {
|
||||
assert.equal(data, "The message from server");
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should publish event on private channel', function (done) {
|
||||
let pusher_a = getPusher(AUTH);
|
||||
let pusher_b = getPusher(AUTH);
|
||||
|
||||
}); // events
|
||||
let channel_a = pusher_a.subscribe('private-messages');
|
||||
let channel_b = pusher_b.subscribe('private-messages');
|
||||
|
||||
channel_a.bind("pusher:subscription_succeeded", function () {
|
||||
console.log("channel_a connected");
|
||||
let xhttp = new XMLHttpRequest();
|
||||
xhttp.open("GET", "/trigger", true);
|
||||
xhttp.send();
|
||||
});
|
||||
|
||||
channel_b.bind("pusher:subscription_succeeded", function () {
|
||||
console.log("channel_b connected");
|
||||
});
|
||||
|
||||
channel_b.bind("messages", function (data) {
|
||||
assert.equal(data, "The message from server");
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
}); // events
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user