|
| 1 | +var expect = require("chai").expect; |
| 2 | +var Cloudevent = require("../index.js"); |
| 3 | +var nock = require("nock"); |
| 4 | + |
| 5 | +const type = "com.github.pull.create"; |
| 6 | +const source = "urn:event:from:myapi/resourse/123"; |
| 7 | +const webhook = "https://cloudevents.io/webhook"; |
| 8 | +const contentType = "application/cloudevents+json; charset=utf-8"; |
| 9 | +const now = new Date(); |
| 10 | +const schemaurl = "http://cloudevents.io/schema.json"; |
| 11 | + |
| 12 | +const ceContentType = "application/json"; |
| 13 | + |
| 14 | +const data = { |
| 15 | + foo: "bar" |
| 16 | +}; |
| 17 | + |
| 18 | +const Structured01 = Cloudevent.bindings["http-structured0.1"]; |
| 19 | +const Binary02 = Cloudevent.bindings["http-binary0.2"]; |
| 20 | + |
| 21 | +var cloudevent = |
| 22 | + new Cloudevent() |
| 23 | + .type(type) |
| 24 | + .source(source) |
| 25 | + .contenttype(ceContentType) |
| 26 | + .time(now) |
| 27 | + .schemaurl(schemaurl) |
| 28 | + .data(data); |
| 29 | + |
| 30 | +cloudevent.eventTypeVersion("1.0.0"); |
| 31 | + |
| 32 | +var httpcfg = { |
| 33 | + method : "POST", |
| 34 | + url : webhook + "/json" |
| 35 | +}; |
| 36 | + |
| 37 | +var httpstructured01 = new Structured01(httpcfg); |
| 38 | +var httpbinary02 = new Binary02(httpcfg); |
| 39 | + |
| 40 | +describe("HTTP Transport Binding - Version 0.2", () => { |
| 41 | + beforeEach(() => { |
| 42 | + // Mocking the webhook |
| 43 | + nock(webhook) |
| 44 | + .post("/json") |
| 45 | + .reply(201, {status: "accepted"}); |
| 46 | + }); |
| 47 | + |
| 48 | + describe("Structured", () => { |
| 49 | + describe("JSON Format", () => { |
| 50 | + it("requires '" + contentType + "' Content-Type in the header", () => { |
| 51 | + return httpstructured01.emit(cloudevent) |
| 52 | + .then((response) => { |
| 53 | + expect(response.config.headers["Content-Type"]) |
| 54 | + .to.equal(contentType); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it("the request payload should be correct", () => { |
| 59 | + return httpstructured01.emit(cloudevent) |
| 60 | + .then((response) => { |
| 61 | + expect(JSON.parse(response.config.data)) |
| 62 | + .to.deep.equal(cloudevent.format()); |
| 63 | + }); |
| 64 | + }); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe("Binary", () => { |
| 69 | + describe("JSON Format", () => { |
| 70 | + it("requires '" + cloudevent.getContenttype() + "' Content-Type in the header", () => { |
| 71 | + return httpbinary02.emit(cloudevent) |
| 72 | + .then((response) => { |
| 73 | + expect(response.config.headers["Content-Type"]) |
| 74 | + .to.equal(cloudevent.getContenttype()); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it("the request payload should be correct", () => { |
| 79 | + return httpbinary02.emit(cloudevent) |
| 80 | + .then((response) => { |
| 81 | + expect(JSON.parse(response.config.data)) |
| 82 | + .to.deep.equal(cloudevent.getData()); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it("HTTP Header contains 'ce-type'", () => { |
| 87 | + return httpbinary02.emit(cloudevent) |
| 88 | + .then((response) => { |
| 89 | + expect(response.config.headers) |
| 90 | + .to.have.property("ce-type"); |
| 91 | + }); |
| 92 | + }); |
| 93 | + it("HTTP Header contains 'ce-specversion'", () => { |
| 94 | + return httpbinary02.emit(cloudevent) |
| 95 | + .then((response) => { |
| 96 | + expect(response.config.headers) |
| 97 | + .to.have.property("ce-specversion"); |
| 98 | + }); |
| 99 | + }); |
| 100 | + it("HTTP Header contains 'ce-source'", () => { |
| 101 | + return httpbinary02.emit(cloudevent) |
| 102 | + .then((response) => { |
| 103 | + expect(response.config.headers) |
| 104 | + .to.have.property("ce-source"); |
| 105 | + }); |
| 106 | + }); |
| 107 | + it("HTTP Header contains 'ce-id'", () => { |
| 108 | + return httpbinary02.emit(cloudevent) |
| 109 | + .then((response) => { |
| 110 | + expect(response.config.headers) |
| 111 | + .to.have.property("ce-id"); |
| 112 | + }); |
| 113 | + }); |
| 114 | + it("HTTP Header contains 'ce-time'", () => { |
| 115 | + return httpbinary02.emit(cloudevent) |
| 116 | + .then((response) => { |
| 117 | + expect(response.config.headers) |
| 118 | + .to.have.property("ce-time"); |
| 119 | + }); |
| 120 | + }); |
| 121 | + it("HTTP Header contains 'ce-schemaurl'", () => { |
| 122 | + return httpbinary02.emit(cloudevent) |
| 123 | + .then((response) => { |
| 124 | + expect(response.config.headers) |
| 125 | + .to.have.property("ce-schemaurl"); |
| 126 | + }); |
| 127 | + }); |
| 128 | + }); |
| 129 | + }); |
| 130 | +}); |
0 commit comments