예제 #1
0
파일: error.go 프로젝트: cxfksword/mocku
func (c *ErrorController) parseJavascriptContent(content string) (string, error) {
	if !strings.HasPrefix(content, "//javascript") {
		return "", fmt.Errorf("not javascript content")
	}
	content = strings.TrimSpace(strings.TrimPrefix(content, "//javascript"))

	rawUrl := c.Ctx.Input.Scheme() + "://" + c.Ctx.Request.Host + c.Ctx.Request.RequestURI

	request := utils.ToJson(map[string]interface{}{
		"params":  c.Input(),
		"host":    c.Ctx.Request.Host,
		"url":     rawUrl,
		"headers": c.Ctx.Request.Header,
	})

	var body []byte
	if strings.Contains(content, "$response.body") {
		// get origin request's response body
		finish := make(chan bool)
		go func() {
			client := newProxyHttpServer()
			client.OnResponse().DoFunc(func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
				body, _ = ioutil.ReadAll(resp.Body)
				finish <- true
				return resp
			})
			client.ServeHTTP(c.Ctx.ResponseWriter.ResponseWriter, c.Ctx.Request)
		}()
		<-finish
	}
	response := utils.ToJson(map[string]interface{}{
		"body": string(body),
	})

	l.Lock()
	defer l.Unlock()
	_, err := vm.Run(fmt.Sprintf(`
    $request=%s;
    $response=%s;

    %s
    `, request, response, content))
	if err != nil {
		return "", err
	}

	if value, err := vm.Get("$output"); err == nil {
		if content, err := value.ToString(); err == nil {
			return content, nil
		}
	}

	return "", err
}
예제 #2
0
파일: models.go 프로젝트: cxfksword/mocku
func (this *MockRule) Bind(c ResolveParameter) {
	id, _ := c.GetInt("id")
	projectId, _ := c.GetInt("projectId")
	url := c.GetString("url")
	title := c.GetString("title")
	method := c.GetString("method")
	templateDir := c.GetString("templateDir")
	templateFile := c.GetString("templateFile")
	backendUrl := c.GetString("backendUrl")

	headers := []Pair{}
	params := []Pair{}
	for i := 0; i < 100; i++ {
		key := c.GetString(fmt.Sprintf("headers[%d][key]", i))
		if key == "" {
			break
		}

		value := c.GetString(fmt.Sprintf("headers[%d][value]", i))
		headers = append(headers, Pair{Key: key, Value: value})
	}

	for i := 0; i < 100; i++ {
		key := c.GetString(fmt.Sprintf("params[%d][key]", i))
		if key == "" {
			break
		}

		value := c.GetString(fmt.Sprintf("params[%d][value]", i))
		params = append(params, Pair{Key: key, Value: value})
	}

	if this.Id <= 0 {
		this.Id = id
	}
	if this.ProjectId <= 0 {
		this.ProjectId = projectId
	}

	this.Url = url
	this.Title = title
	this.Method = method
	this.Url = url
	this.TemplateDir = templateDir
	this.TemplateFile = templateFile
	this.BackendUrl = backendUrl
	this.Headers = utils.ToJson(headers)
	this.Params = utils.ToJson(params)
	this.HeaderPair = headers
	this.ParamPair = params
	this.Enable = true

	var contents []*MockContent
	for i := 0; i < 100; i++ {
		val := c.GetString(fmt.Sprintf("contents[%d][id]", i))
		if val == "" {
			break
		}

		var content MockContent
		content.Id, _ = c.GetInt(fmt.Sprintf("contents[%d][id]", i))
		content.RuleId, _ = c.GetInt(fmt.Sprintf("contents[%d][ruleId]", i))
		content.Tag = c.GetString(fmt.Sprintf("contents[%d][tag]", i))
		content.DelaySeconds, _ = c.GetInt(fmt.Sprintf("contents[%d][delaySeconds]", i))
		content.Content = c.GetString(fmt.Sprintf("contents[%d][content]", i))
		contents = append(contents, &content)
	}
	this.Contents = contents
}