func TestDefault(t *testing.T) {
	o, err := option.New([]string{
		"-s", "AAAA",
		"-i", "BBBB",
		"-r", "CCCC",
		"-b", "DDDD",
	})
	if err != nil {
		t.Fatal(err)
	}

	if o.SecretAccessKey != "AAAA" {
		t.Error("wrong AWSSecretAccessKey:", o.SecretAccessKey)
	}
	if o.AccessKeyID != "BBBB" {
		t.Error("wrong AWSAccessKeyID:", o.AccessKeyID)
	}
	if o.Region != "CCCC" {
		t.Error("wrong AWSRegion:", o.Region)
	}
	if o.Bucket != "DDDD" {
		t.Error("wrong AWSS3Bucket:", o.Bucket)
	}
	if o.Port != 80 {
		t.Error("wrong Port:", o.Port)
	}
	if int(o.Duration) != int(time.Minute) {
		t.Error("wrong Duration:", o.Duration)
	}
}
func TestUpload(t *testing.T) {
	o, err := option.New([]string{
		"-b", Bucket,
	})
	if err != nil {
		t.Fatal(err)
	}

	content := []byte("foo")
	p := publisher.Publisher{"text/plain", len(content)}
	form, err := p.Publish(o.AccessKeyID, o.SecretAccessKey, o.Bucket, o.Duration)
	if err != nil {
		t.Fatal(err)
	}
	var reqBody bytes.Buffer
	w := multipart.NewWriter(&reqBody)
	for fieldname, value := range form.Fields {
		err := w.WriteField(fieldname, value)
		if err != nil {
			t.Fatal(err)
		}
	}
	part, err := w.CreateFormFile("file", "foo.txt")
	if err != nil {
		t.Fatal(err)
	}
	_, err = part.Write(content)
	if err != nil {
		t.Fatal(err)
	}
	err = w.Close()
	if err != nil {
		t.Fatal(err)
	}

	req, err := http.NewRequest("POST", form.URL, &reqBody)
	if err != nil {
		t.Fatal(err)
	}
	req.Header.Set("Content-Type", w.FormDataContentType())
	// req.Header.Set("Content-Length", fmt.Sprintf("%d", reqBody.Len()))
	fmt.Println("REQUEST+++++")
	fmt.Printf("%+v", req)
	fmt.Println("+++++++++++++")

	client := &http.Client{}
	r, err := client.Do(req)
	if err != nil {
		t.Fatal(err)
	}
	resBody, err := ioutil.ReadAll(r.Body)
	if err != nil {
		t.Fatal(err)
	}
	fmt.Println("RESPONSE+++++")
	fmt.Println(r.StatusCode)
	fmt.Println(string(resBody))
	fmt.Println("+++++++++++++")
}
Example #3
0
func main() {
	o, err := option.New(os.Args[1:])
	if err != nil {
		os.Exit(1)
	}

	err = server.Serve(o)
	if err != nil {
		log.Fatal(err)
	}
}
func TestUnknown(t *testing.T) {
	_, err := option.New([]string{"-x"})
	if err == nil {
		t.Error("should error with unknown flags")
	}
}