Created
July 19, 2022 10:17
-
-
Save nidhi-canopas/7bebba72d073b1ac72f6722dacbeee4d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func MockJsonPost(c *gin.Context, content interface{}) { | |
| c.Request.Method = "POST" | |
| c.Request.Header.Set("Content-Type", "application/json") | |
| c.Set("user_id", 1) | |
| jsonbytes, err := json.Marshal(content) | |
| if err != nil { | |
| panic(err) | |
| } | |
| // the request body must be an io.ReadCloser | |
| // the bytes buffer though doesn't implement io.Closer, | |
| // so you wrap it in a no-op closer | |
| c.Request.Body = io.NopCloser(bytes.NewBuffer(jsonbytes)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment