Skip to content

Commit

Permalink
move formcontent to om
Browse files Browse the repository at this point in the history
related to issue #196, I created the formcontent in my repo, now there is a
request to move the code into a pivotal repo to remove the dependency on a
personal repo.

[#159835480]
  • Loading branch information
fredwangwang authored and JT Archie committed Oct 15, 2018
1 parent 251b64c commit 7fcf10b
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 222 deletions.
9 changes: 0 additions & 9 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ required=[
name = "github.com/pivotal-cf/kiln"
branch = "master"

[[constraint]]
name = "github.com/fredwangwang/formcontent"
branch = "threaded"
[[constraint]]
name = "github.com/onsi/ginkgo"
version = "1.4.0"
version = "1.4.0"
2 changes: 1 addition & 1 deletion commands/fakes/multipart.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion commands/import_installation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"io/ioutil"
"strings"

"github.com/fredwangwang/formcontent"
"github.com/pivotal-cf/jhanda"
"github.com/pivotal-cf/om/api"
"github.com/pivotal-cf/om/commands"
"github.com/pivotal-cf/om/commands/fakes"
"github.com/pivotal-cf/om/formcontent"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down
2 changes: 1 addition & 1 deletion commands/upload_product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"os"
"strings"

"github.com/fredwangwang/formcontent"
"github.com/pivotal-cf/jhanda"
"github.com/pivotal-cf/om/api"
"github.com/pivotal-cf/om/commands"
"github.com/pivotal-cf/om/commands/fakes"
"github.com/pivotal-cf/om/extractor"
"github.com/pivotal-cf/om/formcontent"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down
2 changes: 1 addition & 1 deletion commands/upload_stemcell.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"path/filepath"

"github.com/fredwangwang/formcontent"
"github.com/pivotal-cf/jhanda"
"github.com/pivotal-cf/om/api"
"github.com/pivotal-cf/om/formcontent"
"github.com/pivotal-cf/om/validator"

"strconv"
Expand Down
2 changes: 1 addition & 1 deletion commands/upload_stemcell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"os"
"strings"

"github.com/fredwangwang/formcontent"
"github.com/pivotal-cf/jhanda"
"github.com/pivotal-cf/om/api"
"github.com/pivotal-cf/om/commands"
"github.com/pivotal-cf/om/commands/fakes"
"github.com/pivotal-cf/om/formcontent"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type ContentSubmission struct {
ContentLength int64
}

func NewForm() (*Form) {
func NewForm() *Form {
buf := &bytes.Buffer{}

pr, pw := io.Pipe()
Expand Down Expand Up @@ -80,7 +80,7 @@ func (f *Form) AddFile(key string, path string) error {
return nil
}

func (f *Form) Finalize() (ContentSubmission) {
func (f *Form) Finalize() ContentSubmission {
f.formWriter.Close()

// add the length of form fields, including trailing boundary
Expand Down
13 changes: 13 additions & 0 deletions formcontent/formcontent_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package formcontent_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestFormcontent(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Formcontent Suite")
}
174 changes: 174 additions & 0 deletions formcontent/formcontent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package formcontent_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/pivotal-cf/om/formcontent"

"io/ioutil"
"os"
)

var _ = Describe("Formcontent", func() {
var form *formcontent.Form

Describe("AddFile", func() {
var (
fileWithContent1 string
fileWithContent2 string
)

BeforeEach(func() {
handle1, err := ioutil.TempFile("", "")
Expect(err).NotTo(HaveOccurred())

_, err = handle1.WriteString("some content")
Expect(err).NotTo(HaveOccurred())

fileWithContent1 = handle1.Name()

handle2, err := ioutil.TempFile("", "")
Expect(err).NotTo(HaveOccurred())

_, err = handle2.WriteString("some more content")
Expect(err).NotTo(HaveOccurred())

fileWithContent2 = handle2.Name()

form = formcontent.NewForm()
})

AfterEach(func() {
os.Remove(fileWithContent1)
os.Remove(fileWithContent2)
})

It("writes out the provided file as a multipart form using the writer", func() {
err := form.AddFile("something[file1]", fileWithContent1)
Expect(err).NotTo(HaveOccurred())

err = form.AddFile("something[file2]", fileWithContent2)
Expect(err).NotTo(HaveOccurred())

submission := form.Finalize()

content, err := ioutil.ReadAll(submission.Content)
Expect(err).NotTo(HaveOccurred())

Expect(string(content)).To(MatchRegexp(`^--\w+\r\nContent-Disposition: form-data; name=\"something\[file1\]\"; filename=\"\w+\"\r\n` +
`Content-Type: application/octet-stream\r\n\r\n` +
`some content` +
`\r\n--\w+\r\nContent-Disposition: form-data; name=\"something\[file2\]\"; filename=\"\w+\"\r\n` +
`Content-Type: application/octet-stream\r\n\r\n` +
`some more content` +
`\r\n--\w+--\r\n$`))
})

Context("when the file provided is empty", func() {
It("returns an error", func() {
emptyFile, err := ioutil.TempFile("", "")
Expect(err).NotTo(HaveOccurred())

form := formcontent.NewForm()

err = form.AddFile("foo", emptyFile.Name())
Expect(err).To(MatchError("file provided has no content"))
})
})

Context("when an error occurs", func() {
Context("when the original file cannot be read", func() {
It("returns an error", func() {
form := formcontent.NewForm()

err := form.AddFile("foo", "/file/does/not/exist")
Expect(err).To(MatchError(ContainSubstring("no such file or directory")))
})
})
})
})

Describe("AddField", func() {
BeforeEach(func() {
form = formcontent.NewForm()
})

It("writes out the provided fields into the multipart form using the writer", func() {
err := form.AddField("key1", "value1")
Expect(err).NotTo(HaveOccurred())

err = form.AddField("key2", "value2")
Expect(err).NotTo(HaveOccurred())

submission := form.Finalize()

content, err := ioutil.ReadAll(submission.Content)
Expect(err).NotTo(HaveOccurred())

Expect(string(content)).To(MatchRegexp(`^--\w+\r\nContent-Disposition: form-data; name="key1"\r\n\r\nvalue1` +
`\r\n--\w+\r\nContent-Disposition: form-data; name="key2"\r\n\r\nvalue2` +
`\r\n--\w+--\r\n$`))
})
})

Describe("AddCombined", func() {
var fileWithContent1 string

BeforeEach(func() {
var err error

handle1, err := ioutil.TempFile("", "")
Expect(err).NotTo(HaveOccurred())

_, err = handle1.WriteString("some content")
Expect(err).NotTo(HaveOccurred())

fileWithContent1 = handle1.Name()

form = formcontent.NewForm()
})

AfterEach(func() {
os.Remove(fileWithContent1)
})

It("writes out the provided fields into the multipart form using the writer", func() {
err := form.AddField("key1", "value1")
Expect(err).NotTo(HaveOccurred())

err = form.AddFile("file1", fileWithContent1)
Expect(err).NotTo(HaveOccurred())

submission := form.Finalize()

content, err := ioutil.ReadAll(submission.Content)
Expect(err).NotTo(HaveOccurred())

Expect(submission.ContentLength).To(Equal(int64(373)))
Expect(string(content)).To(MatchRegexp(`^--\w+\r\nContent-Disposition: form-data; name=\"file1\"; filename=\"\w+\"\r\n` +
`Content-Type: application/octet-stream\r\n\r\n` +
`some content` +
`\r\n--\w+\r\nContent-Disposition: form-data; name="key1"\r\n\r\nvalue1` +
`\r\n--\w+--\r\n$`))
})
})

Describe("Finalize", func() {
var form *formcontent.Form

BeforeEach(func() {
form = formcontent.NewForm()
})

It("returns a content submission which includes the correct length and content type", func() {
err := form.AddField("key1", "value1")
Expect(err).NotTo(HaveOccurred())

submission := form.Finalize()

Expect(submission.ContentLength).To(Equal(int64(185)))
Expect(submission.ContentType).To(ContainSubstring("multipart/form-data"))
})

})
})
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
"github.com/olekukonko/tablewriter"
"gopkg.in/yaml.v2"

"github.com/fredwangwang/formcontent"
"github.com/pivotal-cf/jhanda"
"github.com/pivotal-cf/om/api"
"github.com/pivotal-cf/om/commands"
"github.com/pivotal-cf/om/extractor"
"github.com/pivotal-cf/om/formcontent"
"github.com/pivotal-cf/om/network"
"github.com/pivotal-cf/om/presenters"
"github.com/pivotal-cf/om/progress"
Expand Down
Loading

0 comments on commit 7fcf10b

Please sign in to comment.