Skip to content

Commit

Permalink
Reverts changes that added headers to the Drop function.
Browse files Browse the repository at this point in the history
- After reconsideration we came to the conclusion that it does not make
  sense to support custom headers at this time and they unnecessarily
  dirty the API.

- We consider downloading to only be part of Drop() main functionality the
  other half being detecting on disk depenencies as well, the headers
  change only affected half of Drop() functionality which is a weird API
  to enforce.
  • Loading branch information
thitch97 authored and ryanmoran committed Jun 30, 2020
1 parent c0bc35a commit f099442
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 65 deletions.
5 changes: 2 additions & 3 deletions cargo/dependency_cacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cargo
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
Expand All @@ -13,7 +12,7 @@ import (

//go:generate faux --interface Downloader --output fakes/downloader.go
type Downloader interface {
Drop(root, uri string, header http.Header) (io.ReadCloser, error)
Drop(root, uri string) (io.ReadCloser, error)
}

type DependencyCacher struct {
Expand Down Expand Up @@ -41,7 +40,7 @@ func (dc DependencyCacher) Cache(root string, deps []ConfigMetadataDependency) (
dc.logger.Subprocess("%s (%s) [%s]", dep.ID, dep.Version, strings.Join(dep.Stacks, ", "))
dc.logger.Action("↳ dependencies/%s", dep.SHA256)

source, err := dc.downloader.Drop("", dep.URI, nil)
source, err := dc.downloader.Drop("", dep.URI)
if err != nil {
return nil, fmt.Errorf("failed to download dependency: %s", err)
}
Expand Down
3 changes: 1 addition & 2 deletions cargo/dependency_cacher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -35,7 +34,7 @@ func testDependencyCacher(t *testing.T, context spec.G, it spec.S) {
Expect(err).NotTo(HaveOccurred())

downloader = &fakes.Downloader{}
downloader.DropCall.Stub = func(root, uri string, header http.Header) (io.ReadCloser, error) {
downloader.DropCall.Stub = func(root, uri string) (io.ReadCloser, error) {
switch uri {
case "http://dep1-uri":
return ioutil.NopCloser(strings.NewReader("dep1-contents")), nil
Expand Down
13 changes: 5 additions & 8 deletions cargo/fakes/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fakes

import (
"io"
"net/http"
"sync"
)

Expand All @@ -11,27 +10,25 @@ type Downloader struct {
sync.Mutex
CallCount int
Receives struct {
Root string
Uri string
Header http.Header
Root string
Uri string
}
Returns struct {
ReadCloser io.ReadCloser
Error error
}
Stub func(string, string, http.Header) (io.ReadCloser, error)
Stub func(string, string) (io.ReadCloser, error)
}
}

func (f *Downloader) Drop(param1 string, param2 string, param3 http.Header) (io.ReadCloser, error) {
func (f *Downloader) Drop(param1 string, param2 string) (io.ReadCloser, error) {
f.DropCall.Lock()
defer f.DropCall.Unlock()
f.DropCall.CallCount++
f.DropCall.Receives.Root = param1
f.DropCall.Receives.Uri = param2
f.DropCall.Receives.Header = param3
if f.DropCall.Stub != nil {
return f.DropCall.Stub(param1, param2, param3)
return f.DropCall.Stub(param1, param2)
}
return f.DropCall.Returns.ReadCloser, f.DropCall.Returns.Error
}
11 changes: 2 additions & 9 deletions cargo/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ import (
"strings"
)

type Transport struct{
header http.Header
}
type Transport struct{}

func NewTransport() Transport {
return Transport{}
}

func (t Transport) Drop(root, uri string, header http.Header) (io.ReadCloser, error) {
func (t Transport) Drop(root, uri string) (io.ReadCloser, error) {
if strings.HasPrefix(uri, "file://") {
file, err := os.Open(filepath.Join(root, strings.TrimPrefix(uri, "file://")))
if err != nil {
Expand All @@ -32,15 +30,10 @@ func (t Transport) Drop(root, uri string, header http.Header) (io.ReadCloser, er
return nil, fmt.Errorf("failed to parse request uri: %s", err)
}

if header != nil {
request.Header = header
}

response, err := http.DefaultClient.Do(request)
if err != nil {
return nil, fmt.Errorf("failed to make request: %s", err)
}

return response.Body, nil
}

42 changes: 10 additions & 32 deletions cargo/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,11 @@ func testTransport(t *testing.T, context spec.G, it spec.S) {

it.Before(func() {
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.Header.Get("header") == "some-header" {
switch req.URL.Path {
case "/some-bundle-with-header":
w.Write([]byte("some-bundle-with-header-contents"))
default:
http.NotFound(w, req)
}
} else {
switch req.URL.Path {
case "/some-bundle":
w.Write([]byte("some-bundle-contents"))
default:
http.NotFound(w, req)
}
switch req.URL.Path {
case "/some-bundle":
w.Write([]byte("some-bundle-contents"))
default:
http.NotFound(w, req)
}
}))
})
Expand All @@ -53,7 +44,7 @@ func testTransport(t *testing.T, context spec.G, it spec.S) {
})

it("downloads the file from a URI", func() {
bundle, err := transport.Drop("", fmt.Sprintf("%s/some-bundle", server.URL), nil)
bundle, err := transport.Drop("", fmt.Sprintf("%s/some-bundle", server.URL))
Expect(err).NotTo(HaveOccurred())

contents, err := ioutil.ReadAll(bundle)
Expand All @@ -63,23 +54,10 @@ func testTransport(t *testing.T, context spec.G, it spec.S) {
Expect(bundle.Close()).To(Succeed())
})

context("when there are request headers", func() {
it("downloads the file from a URI", func() {
bundle, err := transport.Drop("", fmt.Sprintf("%s/some-bundle-with-header", server.URL), http.Header{"header": []string{"some-header"}})
Expect(err).NotTo(HaveOccurred())

contents, err := ioutil.ReadAll(bundle)
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(Equal("some-bundle-with-header-contents"))

Expect(bundle.Close()).To(Succeed())
})
})

context("failure cases", func() {
context("when the uri is malformed", func() {
it("returns an error", func() {
_, err := transport.Drop("", "%%%%", nil)
_, err := transport.Drop("", "%%%%")
Expect(err).To(MatchError(ContainSubstring("failed to parse request uri")))
Expect(err).To(MatchError(ContainSubstring("invalid URL escape")))
})
Expand All @@ -91,7 +69,7 @@ func testTransport(t *testing.T, context spec.G, it spec.S) {
})

it("returns an error", func() {
_, err := transport.Drop("", fmt.Sprintf("%s/some-bundle", server.URL), nil)
_, err := transport.Drop("", fmt.Sprintf("%s/some-bundle", server.URL))
Expect(err).To(MatchError(ContainSubstring("failed to make request")))
Expect(err).To(MatchError(ContainSubstring("connection refused")))
})
Expand Down Expand Up @@ -121,7 +99,7 @@ func testTransport(t *testing.T, context spec.G, it spec.S) {
})

it("returns the file descriptor", func() {
bundle, err := transport.Drop(dir, fmt.Sprintf("file://%s", path), nil)
bundle, err := transport.Drop(dir, fmt.Sprintf("file://%s", path))
Expect(err).NotTo(HaveOccurred())

contents, err := ioutil.ReadAll(bundle)
Expand All @@ -138,7 +116,7 @@ func testTransport(t *testing.T, context spec.G, it spec.S) {

context("when the file does not exist", func() {
it("returns an error", func() {
_, err := transport.Drop(dir, fmt.Sprintf("file://%s", path), nil)
_, err := transport.Drop(dir, fmt.Sprintf("file://%s", path))
Expect(err).To(MatchError(ContainSubstring("failed to open file")))
Expect(err).To(MatchError(ContainSubstring("no such file or directory")))
})
Expand Down
13 changes: 5 additions & 8 deletions postal/fakes/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fakes

import (
"io"
"net/http"
"sync"
)

Expand All @@ -11,27 +10,25 @@ type Transport struct {
sync.Mutex
CallCount int
Receives struct {
Root string
Uri string
Header http.Header
Root string
Uri string
}
Returns struct {
ReadCloser io.ReadCloser
Error error
}
Stub func(string, string, http.Header) (io.ReadCloser, error)
Stub func(string, string) (io.ReadCloser, error)
}
}

func (f *Transport) Drop(param1 string, param2 string, param3 http.Header) (io.ReadCloser, error) {
func (f *Transport) Drop(param1 string, param2 string) (io.ReadCloser, error) {
f.DropCall.Lock()
defer f.DropCall.Unlock()
f.DropCall.CallCount++
f.DropCall.Receives.Root = param1
f.DropCall.Receives.Uri = param2
f.DropCall.Receives.Header = param3
if f.DropCall.Stub != nil {
return f.DropCall.Stub(param1, param2, param3)
return f.DropCall.Stub(param1, param2)
}
return f.DropCall.Returns.ReadCloser, f.DropCall.Returns.Error
}
5 changes: 2 additions & 3 deletions postal/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package postal
import (
"fmt"
"io"
"net/http"
"sort"

"github.com/Masterminds/semver"
Expand All @@ -16,7 +15,7 @@ import (
// Transport serves as the interface for types that can fetch dependencies
// given a location uri using either the http:// or file:// scheme.
type Transport interface {
Drop(root, uri string, header http.Header) (io.ReadCloser, error)
Drop(root, uri string) (io.ReadCloser, error)
}

// Service provides a mechanism for resolving and installing dependencies given
Expand Down Expand Up @@ -97,7 +96,7 @@ func (s Service) Resolve(path, id, version, stack string) (Dependency, error) {
// Dependency and will error if there are inconsistencies in the fetched
// result.
func (s Service) Install(dependency Dependency, cnbPath, layerPath string) error {
bundle, err := s.transport.Drop(cnbPath, dependency.URI, nil)
bundle, err := s.transport.Drop(cnbPath, dependency.URI)
if err != nil {
return fmt.Errorf("failed to fetch dependency: %s", err)
}
Expand Down

0 comments on commit f099442

Please sign in to comment.