当前位置: 首页>>代码示例>>Golang>>正文


Golang Request.PostForm方法代码示例

本文整理汇总了Golang中net/http.Request.PostForm方法的典型用法代码示例。如果您正苦于以下问题:Golang Request.PostForm方法的具体用法?Golang Request.PostForm怎么用?Golang Request.PostForm使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在net/http.Request的用法示例。


在下文中一共展示了Request.PostForm方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Test_HookProvider_TransformRequest

func Test_HookProvider_TransformRequest(t *testing.T) {
	provider := HookProvider{}

	t.Log("Should be OK")
	{
		request := http.Request{
			Header: http.Header{
				"Content-Type": {"application/x-www-form-urlencoded"},
			},
		}
		form := url.Values{}
		form.Add("trigger_word", "bitrise:")
		form.Add("text", "bitrise: branch:master")
		request.PostForm = form

		hookTransformResult := provider.TransformRequest(&request)
		require.NoError(t, hookTransformResult.Error)
		require.False(t, hookTransformResult.ShouldSkip)
		require.Equal(t, []bitriseapi.TriggerAPIParamsModel{
			{
				BuildParams: bitriseapi.BuildParamsModel{
					Branch:       "master",
					Environments: []bitriseapi.EnvironmentItem{},
				},
			},
		}, hookTransformResult.TriggerAPIParams)
	}

	t.Log("Unsupported Event Type")
	{
		request := http.Request{
			Header: http.Header{
				"Content-Type": {"application/json"},
			},
		}
		hookTransformResult := provider.TransformRequest(&request)
		require.False(t, hookTransformResult.ShouldSkip)
		require.EqualError(t, hookTransformResult.Error, "Content-Type is not supported: application/json")
	}

	t.Log("Missing 'text' from request data")
	{
		request := http.Request{
			Header: http.Header{
				"Content-Type": {"application/x-www-form-urlencoded"},
			},
		}
		form := url.Values{}
		form.Add("trigger_word", "the trigger word")
		request.PostForm = form

		hookTransformResult := provider.TransformRequest(&request)
		require.False(t, hookTransformResult.ShouldSkip)
		require.EqualError(t, hookTransformResult.Error, "Failed to parse the request/message: 'trigger_word' parameter found, but 'text' parameter is missing or empty")
	}
}
开发者ID:bitrise-io,项目名称:bitrise-webhooks,代码行数:56,代码来源:slack_test.go

示例2: Test_getInputTextFromFormRequest

func Test_getInputTextFromFormRequest(t *testing.T) {
	t.Log("Proper Form content")
	{
		request := http.Request{}
		form := url.Values{}
		form.Add("trigger_word", "the trigger word")
		form.Add("text", "the trigger word        the text")
		request.PostForm = form

		text, err := getInputTextFromFormRequest(&request)
		require.NoError(t, err)
		require.Equal(t, "the text", text)
	}

	t.Log("Missing trigger_word")
	{
		request := http.Request{}
		form := url.Values{}
		form.Add("text", "the text")
		request.PostForm = form

		text, err := getInputTextFromFormRequest(&request)
		require.EqualError(t, err, "Missing required parameter: either 'command' or 'trigger_word' should be specified")
		require.Equal(t, "", text)
	}

	t.Log("Missing text - trigger_word")
	{
		request := http.Request{}
		form := url.Values{}
		form.Add("trigger_word", "the trigger word")
		request.PostForm = form

		text, err := getInputTextFromFormRequest(&request)
		require.EqualError(t, err, "'trigger_word' parameter found, but 'text' parameter is missing or empty")
		require.Equal(t, "", text)
	}

	t.Log("Missing text - command")
	{
		request := http.Request{}
		form := url.Values{}
		form.Add("command", "the-command")
		request.PostForm = form

		text, err := getInputTextFromFormRequest(&request)
		require.EqualError(t, err, "'command' parameter found, but 'text' parameter is missing or empty")
		require.Equal(t, "", text)
	}
}
开发者ID:bitrise-io,项目名称:bitrise-webhooks,代码行数:50,代码来源:slack_test.go

示例3: benchRequest

func benchRequest(b *testing.B, router http.Handler, r *http.Request) {
	w := mockResponseWriter{}
	u := r.URL
	r.RequestURI = u.RequestURI()

	b.ReportAllocs()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		router.ServeHTTP(&w, r)

		// clear caches
		r.Form = nil
		r.PostForm = nil
		r.MultipartForm = nil
	}
}
开发者ID:najeira,项目名称:go-frameworks-benchmark,代码行数:17,代码来源:main.go

示例4: requestDataToUrlValues

func requestDataToUrlValues(r *http.Request, data map[string]interface{}) (err error) {
	var (
		form        = make(url.Values)
		postform    = make(url.Values)
		queryValues url.Values
	)

	makeRequestData("", data, postform)

	for k, v := range postform {
		form[k] = append(form[k], v...)
	}

	if queryValues, err = url.ParseQuery(r.URL.RawQuery); err == nil {
		for k, v := range queryValues {
			form[k] = append(form[k], v...)
		}
	}

	r.PostForm = postform
	r.Form = form

	return
}
开发者ID:zaolab,项目名称:sunnified,代码行数:24,代码来源:context.go


注:本文中的net/http.Request.PostForm方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。