コード例 #1
0
func (this *HoverflyMiddlewareHandler) Put(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
	defer req.Body.Close()

	var middlewareReq MiddlewareView

	body, err := ioutil.ReadAll(req.Body)
	if err != nil {
		handlers.WriteErrorResponse(w, "Malformed JSON", 400)
		return
	}

	err = json.Unmarshal(body, &middlewareReq)
	if err != nil {
		handlers.WriteErrorResponse(w, "Malformed JSON", 400)
		return
	}

	err = this.Hoverfly.SetMiddleware(middlewareReq.Middleware)
	if err != nil {
		handlers.WriteErrorResponse(w, "Invalid middleware: "+err.Error(), 422)
		return
	}

	this.Get(w, req, next)
}
コード例 #2
0
func (this *SimulationHandler) Put(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
	body, _ := ioutil.ReadAll(req.Body)

	var jsonMap map[string]interface{}
	err := json.Unmarshal(body, &jsonMap)
	if err != nil {
		log.WithFields(log.Fields{
			"body": string(body),
		}).Debug(err.Error())

		handlers.WriteErrorResponse(w, "Invalid json", http.StatusBadRequest)
		return
	}

	var simulationView SimulationView

	if path, err := simulationView.GetValidationSchema().Validate(jsonMap); err != nil {
		log.WithFields(log.Fields{
			"body": string(body),
		}).Debug(err.Error())

		handlers.WriteErrorResponse(w, "Json did not match schema: "+path, http.StatusUnprocessableEntity)
		return
	}

	json.Unmarshal(body, &simulationView)

	this.Hoverfly.DeleteSimulation()

	err = this.Hoverfly.PutSimulation(simulationView)
	if err != nil {

		log.WithFields(log.Fields{
			"body": string(body),
		}).Debug(err.Error())

		handlers.WriteErrorResponse(w, "An error occured: "+err.Error(), http.StatusInternalServerError)
		return
	}

	this.Get(w, req, next)
}
コード例 #3
0
func (this *SimulationHandler) Get(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
	simulationView, err := this.Hoverfly.GetSimulation()
	if err != nil {
		handlers.WriteErrorResponse(w, err.Error(), http.StatusInternalServerError)
		return
	}

	bytes, _ := json.Marshal(simulationView)

	handlers.WriteResponse(w, bytes)
}
コード例 #4
0
func (this *HoverflyModeHandler) Put(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	defer r.Body.Close()

	var modeView ModeView

	body, _ := ioutil.ReadAll(r.Body)

	err := json.Unmarshal(body, &modeView)
	if err != nil {
		handlers.WriteErrorResponse(w, "Malformed JSON", 400)
		return
	}

	err = this.Hoverfly.SetMode(modeView.Mode)
	if err != nil {
		handlers.WriteErrorResponse(w, err.Error(), 422)
		return
	}

	this.Get(w, r, next)
}