示例#1
0
	. "github.com/onsi/gomega"
)

var _ = Describe("Routes", func() {
	var muxer web.Muxer

	BeforeEach(func() {
		muxer = web.NewMuxer()
		messages.Routes{
			RequestCounter:                               middleware.RequestCounter{},
			RequestLogging:                               middleware.RequestLogging{},
			DatabaseAllocator:                            middleware.DatabaseAllocator{},
			NotificationsWriteOrEmailsWriteAuthenticator: middleware.Authenticator{Scopes: []string{"notifications.write", "emails.write"}},

			ErrorWriter:   mocks.NewErrorWriter(),
			MessageFinder: mocks.NewMessageFinder(),
		}.Register(muxer)
	})

	It("routes GET /messages/{message_id}", func() {
		request, err := http.NewRequest("GET", "/messages/some-message-id", nil)
		Expect(err).NotTo(HaveOccurred())

		s := muxer.Match(request).(stack.Stack)
		Expect(s.Handler).To(BeAssignableToTypeOf(messages.GetHandler{}))
		ExpectToContainMiddlewareStack(s.Middleware, middleware.RequestLogging{}, middleware.RequestCounter{}, middleware.Authenticator{}, middleware.DatabaseAllocator{})

		authenticator := s.Middleware[2].(middleware.Authenticator)
		Expect(authenticator.Scopes).To(ConsistOf([]string{"notifications.write", "emails.write"}))
	})
})
示例#2
0
var _ = Describe("GetHandler", func() {
	var (
		handler       messages.GetHandler
		errorWriter   *mocks.ErrorWriter
		writer        *httptest.ResponseRecorder
		request       *http.Request
		messageID     string
		err           error
		messageFinder *mocks.MessageFinder
		database      *mocks.Database
		context       stack.Context
	)

	BeforeEach(func() {
		errorWriter = mocks.NewErrorWriter()
		messageFinder = mocks.NewMessageFinder()
		writer = httptest.NewRecorder()
		messageID = "message-123"
		database = mocks.NewDatabase()
		context = stack.NewContext()
		context.Set("database", database)

		request, err = http.NewRequest("GET", "/messages/"+messageID, nil)
		if err != nil {
			panic(err)
		}

		handler = messages.NewGetHandler(messageFinder, errorWriter)
	})

	Describe("ServeHTTP", func() {