コード例 #1
0
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("+++++++++++++")
}
コード例 #2
0
ファイル: index.go プロジェクト: go-microservices/policies
func (i Index) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if !(r.Method == "OPTIONS" || r.Method == "POST") {
		responseError(w, http.StatusMethodNotAllowed, []error{fmt.Errorf("POST method is allowed")})
		return
	}

	header := w.Header()
	header.Set("Access-Control-Allow-Origin", "*")
	header.Set("Access-Control-Allow-Methods", "POST, OPTIONS")
	header.Set("Access-Control-Allow-Headers", "Origin, Content-Type")
	if r.Method == "OPTIONS" {
		w.WriteHeader(http.StatusOK)
		return
	}

	reqBody, err := ioutil.ReadAll(r.Body)
	if err != nil {
		responseError(w, http.StatusBadRequest, []error{err})
		return
	}

	var p publisher.Publisher
	err = json.Unmarshal(reqBody, &p)
	if err != nil {
		responseError(w, http.StatusBadRequest, []error{err})
		return
	}

	form, err := p.Publish(i.Options.AccessKeyID, i.Options.SecretAccessKey, i.Options.Bucket, i.Options.Duration)
	if err != nil {
		responseError(w, http.StatusBadRequest, []error{err})
		return
	}

	response(w, http.StatusOK, form)
}