Esempio n. 1
0
// NewDownloadHandler returns a new HTTP handler for the given args.
func NewDownloadHandler(args apihttp.NewHandlerArgs) http.Handler {
	extractor := &httpDownloadRequestExtractor{
		connect: args.Connect,
	}
	deps := internalserver.NewLegacyHTTPHandlerDeps(extractor)
	return internalserver.NewLegacyHTTPHandler(deps)
}
Esempio n. 2
0
func newUnitResourceHandler(httpCtxt httpContext) http.Handler {
	return internalserver.NewLegacyHTTPHandler(
		func(req *http.Request) (internalserver.UnitDataStore, error) {
			st, ent, err := httpCtxt.stateForRequestAuthenticatedAgent(req)
			if err != nil {
				return nil, errors.Trace(err)
			}
			resources, err := st.Resources()
			if err != nil {
				return nil, errors.Trace(err)
			}

			unit, ok := ent.(resource.Unit)
			if !ok {
				logger.Criticalf("unexpected type: %T", ent)
				return nil, errors.Errorf("unexpected type: %T", ent)
			}

			st2 := &resourceUnitState{
				unit:  unit,
				state: resources,
			}
			return st2, nil
		},
	)
}
Esempio n. 3
0
func (s *LegacyHTTPHandlerSuite) TestIntegration(c *gc.C) {
	opened := resourcetesting.NewResource(c, s.stub, "spam", "a-service", "some data")
	s.store.ReturnOpenResource = opened
	s.deps.ReturnConnect = s.store
	h := server.NewLegacyHTTPHandler(s.deps.Connect)
	req, err := api.NewHTTPDownloadRequest("spam")
	c.Assert(err, jc.ErrorIsNil)
	req.URL, err = url.ParseRequestURI("https://api:17017/units/eggs/1/resources/spam?:resource=spam")
	c.Assert(err, jc.ErrorIsNil)
	resp := &fakeResponseWriter{
		stubResponseWriter: s.resp,
	}

	h.ServeHTTP(resp, req)

	resp.checkWritten(c, "some data", http.Header{
		"Content-Type":   []string{api.ContentTypeRaw},
		"Content-Length": []string{"9"}, // len("some data")
		"Content-Sha384": []string{opened.Fingerprint.String()},
	})
}
Esempio n. 4
0
func (s *LegacyHTTPHandlerSuite) TestNewLegacyHTTPHandler(c *gc.C) {
	h := server.NewLegacyHTTPHandler(s.deps)

	s.stub.CheckNoCalls(c)
	c.Check(h, gc.NotNil)
}