示例#1
0
func VerifyProto(expected proto.Message) http.HandlerFunc {
	return ghttp.CombineHandlers(
		ghttp.VerifyContentType("application/x-protobuf"),

		func(w http.ResponseWriter, req *http.Request) {
			defer GinkgoRecover()
			body, err := ioutil.ReadAll(req.Body)
			Expect(err).ToNot(HaveOccurred())
			req.Body.Close()

			expectedType := reflect.TypeOf(expected)
			actualValuePtr := reflect.New(expectedType.Elem())

			actual, ok := actualValuePtr.Interface().(proto.Message)
			Expect(ok).To(BeTrue())

			err = proto.Unmarshal(body, actual)
			Expect(err).ToNot(HaveOccurred())

			Expect(actual).To(Equal(expected), "ProtoBuf Mismatch")
		},
	)
}
示例#2
0
						},
					}
					data, err := schedulingInfoResponse.Marshal()
					Expect(err).ToNot(HaveOccurred())

					fakeBBS.RouteToHandler("POST", "/v1/desired_lrp_scheduling_infos/list",
						ghttp.RespondWith(200, data, http.Header{bbs.ContentTypeHeader: []string{bbs.ProtoContentType}}),
					)

					fakeBBS.RouteToHandler("POST", "/v1/domains/upsert",
						ghttp.RespondWith(200, `{}`),
					)

					fakeBBS.RouteToHandler("POST", "/v1/desired_lrp/desire",
						ghttp.CombineHandlers(
							ghttp.VerifyContentType("application/x-protobuf"),
							func(w http.ResponseWriter, req *http.Request) {
								body, err := ioutil.ReadAll(req.Body)
								Expect(err).ShouldNot(HaveOccurred())
								defer req.Body.Close()

								protoMessage := &models.DesireLRPRequest{}

								err = proto.Unmarshal(body, protoMessage)
								Expect(err).ToNot(HaveOccurred(), "Failed to unmarshal protobuf")

								Expect(protoMessage.DesiredLrp.ProcessGuid).To(Equal("process-guid-3"))
							},
						),
					)
示例#3
0
		)

		BeforeEach(func() {
			server = ghttp.NewServer()
			authServer = ghttp.NewServer()
			token = uuid.NewUUID().String()
			responseBody := &token_fetcher.Token{
				AccessToken: token,
				ExpireTime:  20,
			}

			authServer.AppendHandlers(
				ghttp.CombineHandlers(
					ghttp.VerifyRequest("POST", "/oauth/token"),
					ghttp.VerifyBasicAuth("some-name", "some-secret"),
					ghttp.VerifyContentType("application/x-www-form-urlencoded; charset=UTF-8"),
					ghttp.VerifyHeader(http.Header{
						"Accept": []string{"application/json; charset=utf-8"},
					}),
					verifyBody("grant_type=client_credentials"),
					ghttp.RespondWithJSONEncoded(http.StatusOK, responseBody),
				))

			server.AppendHandlers(
				ghttp.CombineHandlers(
					ghttp.VerifyRequest("POST", "/v1/routes"),
					ghttp.VerifyHeader(http.Header{
						"Authorization": []string{"bearer " + token},
					}),
					ghttp.RespondWithJSONEncoded(http.StatusOK, nil),
				),
示例#4
0
				Expect(fakeBBS.ReceivedRequests()).To(HaveLen(0))
			})
		})
	})

	Describe("authenticating with the cf realm with a one time code", func() {
		BeforeEach(func() {
			clientConfig = &ssh.ClientConfig{
				User: "******",
				Auth: []ssh.AuthMethod{ssh.Password("abc123")},
			}

			fakeUAA.RouteToHandler("POST", "/oauth/token", ghttp.CombineHandlers(
				ghttp.VerifyRequest("POST", "/oauth/token"),
				ghttp.VerifyBasicAuth("amandaplease", "password1"),
				ghttp.VerifyContentType("application/x-www-form-urlencoded"),
				ghttp.VerifyFormKV("grant_type", "authorization_code"),
				ghttp.VerifyFormKV("code", "abc123"),
				ghttp.RespondWithJSONEncoded(http.StatusOK, authenticators.UAAAuthTokenResponse{
					AccessToken: "proxy-token",
					TokenType:   "bearer",
				}),
			))

			fakeCC.RouteToHandler("GET", "/internal/apps/60f0f26e-86b3-4487-8f19-9e94f848f3d2/ssh_access/99", ghttp.CombineHandlers(
				ghttp.VerifyRequest("GET", "/internal/apps/60f0f26e-86b3-4487-8f19-9e94f848f3d2/ssh_access/99"),
				ghttp.VerifyHeader(http.Header{"Authorization": []string{"bearer proxy-token"}}),
				ghttp.RespondWithJSONEncoded(http.StatusOK, authenticators.AppSSHResponse{
					ProcessGuid: processGuid,
				}),
			))
示例#5
0
				Expect(err).To(HaveOccurred())
			})
		})
	})

	Describe("updating user", func() {
		user := wl.User{
			Name:     "username",
			Revision: 12,
		}

		It("performs PUT requests with correct headers to /user", func() {
			server.AppendHandlers(
				ghttp.CombineHandlers(
					ghttp.VerifyRequest("PUT", "/user"),
					ghttp.VerifyContentType("application/json"),
					ghttp.VerifyHeader(http.Header{
						"X-Access-Token": []string{dummyAccessToken},
						"X-Client-ID":    []string{dummyClientID},
					}),
					// TODO: Add body check here
				),
			)

			client.UpdateUser(wl.User{})

			Expect(server.ReceivedRequests()).Should(HaveLen(1))
		})

		Context("when the request is valid", func() {
			It("returns successfully", func() {
			bodyTestHandler = ghttp.CombineHandlers(
				ghttp.VerifyHeaderKV("X-Amz-Acl", "public-read"),
				func(rw http.ResponseWriter, req *http.Request) {
					defer GinkgoRecover()
					defer req.Body.Close()
					bodyBytes, err := ioutil.ReadAll(req.Body)
					Expect(err).ToNot(HaveOccurred())
					bodyChan <- bodyBytes
				},
				ghttp.RespondWith(http.StatusOK, nil),
			)
			testS3Server.AppendHandlers(
				bodyTestHandler,
				ghttp.CombineHandlers(
					ghttp.VerifyContentType("image/png"),
					bodyTestHandler,
				),
			)

			runnerArgs = Args{
				NumRequests:      12,
				RateLimit:        100,
				StartConcurrency: 2,
				EndConcurrency:   4,
				ConcurrencyStep:  2,
				Proxy:            testServer.URL(),
				URL:              url,
				BucketName:       "blah-bucket",
				Endpoint:         testS3Server.URL(),
				AccessKeyID:      "ABCD",
						bodyChan <- bodyBytes
					},
					ghttp.RespondWith(http.StatusOK, nil),
				))
			})
			AfterEach(func() {
				close(bodyChan)
			})
			It("can upload a publicly-readable file S3 with retries", func() {
				dest, err := uploader.Upload(uploadConfig, file, fileName, false)
				Expect(err).ToNot(HaveOccurred())
				Expect(dest).To(Equal(testS3Server.URL() + "/" + bucketName + "/" + fileName))
				var bodyBytes []byte
				Eventually(bodyChan).Should(Receive(&bodyBytes))
				Expect(string(bodyBytes)).To(Equal("test body"))
			})
		})
		Context("with a content type specified", func() {
			BeforeEach(func() {
				testS3Server.AppendHandlers(ghttp.VerifyContentType("image/png"))
			})
			It("can upload a publicly-readable file S3 with retries", func() {
				dest, err := uploader.Upload(uploadConfig, file, fileName, true)
				Expect(err).ToNot(HaveOccurred())
				Expect(dest).To(Equal(testS3Server.URL() + "/" + bucketName + "/" + fileName))
			})

		})
	})
})