Exemplo n.º 1
0
		verifyRequestResponse := func(server *ghttp.Server, method, uri, token, createdAt string, responseCode int64) {
			var verifier http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
				Expect(r.Method).To(Equal(method))
				Expect(r.RequestURI).To(Equal(uri))
				err := r.ParseForm()
				Expect(err).To(BeNil())
				Expect(r.Header.Get("Authorization")).To(Equal("api-key"))
				Expect(r.PostFormValue("instance[token]")).To(Equal(token))
				Expect(r.PostFormValue("instance[created_at]")).To(Equal(createdAt))
				Expect(r.PostFormValue("format")).To(Equal("json"))

				w.WriteHeader(int(responseCode))
			}

			server.AppendHandlers(verifier)
		}

		Context("successful API request", func() {
			Context("with token and no createdAt", func() {
				It("should return true", func() {
					verifyRequestResponse(server, "POST", "/bots/bot-id/instances", "bot-token", "", 201)
					status := bc.RegisterBot("bot-token", 0)
					Expect(status).To(BeTrue())
				})
			})

			Context("with token and createdAt", func() {
				It("should return true", func() {
					verifyRequestResponse(server, "POST", "/bots/bot-id/instances", "bot-token", "123456789", 201)
					status := bc.RegisterBot("bot-token", 123456789)
		})
	})

	Context("when running in parallel", func() {
		Context("as the first node", func() {
			BeforeEach(func() {
				server.AppendHandlers(ghttp.CombineHandlers(
					ghttp.VerifyRequest("GET", "/RemoteAfterSuiteData"),
					func(writer http.ResponseWriter, request *http.Request) {
						ranThing("Request1")
					},
					ghttp.RespondWithJSONEncoded(200, types.RemoteAfterSuiteData{false}),
				), ghttp.CombineHandlers(
					ghttp.VerifyRequest("GET", "/RemoteAfterSuiteData"),
					func(writer http.ResponseWriter, request *http.Request) {
						ranThing("Request2")
					},
					ghttp.RespondWithJSONEncoded(200, types.RemoteAfterSuiteData{false}),
				), ghttp.CombineHandlers(
					ghttp.VerifyRequest("GET", "/RemoteAfterSuiteData"),
					func(writer http.ResponseWriter, request *http.Request) {
						ranThing("Request3")
					},
					ghttp.RespondWithJSONEncoded(200, types.RemoteAfterSuiteData{true}),
				))

				node = newNode(func() {
					ranThing("A")
				}, func() {
					ranThing("B")
				})
		var parallelNode, parallelTotal int
		BeforeEach(func() {
			ranB = false
			parallelNode, parallelTotal = 1, 3
		})

		Context("as the first node, it runs A", func() {
			var expectedState types.RemoteBeforeSuiteData

			BeforeEach(func() {
				parallelNode, parallelTotal = 1, 3
			})

			JustBeforeEach(func() {
				server.AppendHandlers(ghttp.CombineHandlers(
					ghttp.VerifyRequest("POST", "/BeforeSuiteState"),
					ghttp.VerifyJSONRepresenting(expectedState),
				))

				outcome = node.Run(parallelNode, parallelTotal, server.URL())
			})

			Context("when A succeeds", func() {
				BeforeEach(func() {
					expectedState = types.RemoteBeforeSuiteData{[]byte("my data"), types.RemoteBeforeSuiteStatePassed}

					node = newNode(func() []byte {
						return []byte("my data")
					}, func([]byte) {
						ranB = true
					})
				})