本文整理汇总了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")
}
}
示例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)
}
}
示例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
}
}
示例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
}