예제 #1
0
// ServeHTTP accepts a POST request with a body containing a modifier as a JSON
// message and updates the contained reqmod and resmod with the parsed
// modifier.
func (m *Modifier) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	if req.Method != "POST" {
		rw.Header().Set("Allow", "POST")
		rw.WriteHeader(405)
		return
	}

	body, err := ioutil.ReadAll(req.Body)
	if err != nil {
		http.Error(rw, err.Error(), 500)
		martian.Errorf("error reading request body: %v", err)
		return
	}
	req.Body.Close()

	r, err := parse.FromJSON(body)
	if err != nil {
		http.Error(rw, err.Error(), 400)
		martian.Errorf("error parsing JSON: %v", err)
		return
	}

	m.SetRequestModifier(r.RequestModifier())
	m.SetResponseModifier(r.ResponseModifier())
}
예제 #2
0
// ServeHTTP writes out a JSON response containing a list of verification
// errors that occurred during the requests and responses sent to the proxy.
func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	rw.Header().Set("Content-Type", "application/json")

	if req.Method != "GET" {
		rw.Header().Set("Allow", "GET")
		rw.WriteHeader(405)
		martian.Errorf("Method: %v not allowed for URL: %v. Use GET.", req.Method, req.URL)
		return
	}

	vres := &verifyResponse{}

	if h.reqv != nil {
		if err := h.reqv.VerifyRequests(); err != nil {
			appendError(vres, err)
		}
	}
	if h.resv != nil {
		if err := h.resv.VerifyResponses(); err != nil {
			appendError(vres, err)
		}
	}

	json.NewEncoder(rw).Encode(vres)
}
예제 #3
0
// ServeHTTP resets the verifier for the given ID so that it may
// be run again.
func (h *ResetHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	if req.Method != "POST" {
		rw.Header().Set("Allow", "POST")
		rw.WriteHeader(405)
		martian.Errorf("Method: %v not allowed for URL: %v. Use GET.", req.Method, req.URL)
		return
	}

	if h.reqv != nil {
		h.reqv.ResetRequestVerifications()
	}
	if h.resv != nil {
		h.resv.ResetResponseVerifications()
	}

	rw.WriteHeader(204)
}