예제 #1
0
func (s *Server) DeleteTask(name string) error {
	v := url.Values{}
	v.Add("name", name)
	req, err := http.NewRequest("DELETE", s.URL()+"/task?"+v.Encode(), nil)
	if err != nil {
		return err
	}
	client := &http.Client{}
	r, err := client.Do(req)
	if err != nil {
		return err
	}
	defer r.Body.Close()
	if r.StatusCode != http.StatusOK {
		return fmt.Errorf("unexpected status code got %d exp %d", r.StatusCode, http.StatusOK)
	}
	// Decode valid response
	type resp struct {
		Error string `json:"Error"`
	}
	d := json.NewDecoder(r.Body)
	rp := resp{}
	d.Decode(&rp)
	if rp.Error != "" {
		return errors.New(rp.Error)
	}
	return nil
}
예제 #2
0
func MustHTTP(verb, path string, params, headers map[string]string, body string) (int, string) {
	req, err := http.NewRequest(verb, path, bytes.NewBuffer([]byte(body)))
	if err != nil {
		panic(err)
	}

	if params != nil {
		q := url.Values{}
		for k, v := range params {
			q.Set(k, v)
		}
		req.URL.RawQuery = q.Encode()
	}

	for k, v := range headers {
		req.Header.Set(k, v)
	}
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	b, err := ioutil.ReadAll(resp.Body)
	return resp.StatusCode, strings.TrimRight(string(b), "\n")
}
예제 #3
0
func TestHandler_GzipDisabled(t *testing.T) {
	srvr := OpenAuthlessServer(NewMessagingClient())
	s := NewHTTPServer(srvr)
	defer s.Close()

	req, err := http.NewRequest("GET", s.URL+`/ping`, bytes.NewBuffer([]byte{}))
	if err != nil {
		panic(err)
	}

	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Accept-Encoding", "")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}

	if ce := resp.Header.Get("Content-Encoding"); ce == "gzip" {
		t.Fatalf("unexpected Content-Encoding.  expected %q, actual: %q", "", ce)
	}
}