func request(method, path string, body io.Reader, ct string, e *gin.Engine) (int, string) { r, _ := http.NewRequest(method, path, body) if body != nil && ct != "" { r.Header.Set("Content-Type", ct) } w := httptest.NewRecorder() e.ServeHTTP(w, r) return w.Code, w.Body.String() }
func request(server *gin.Engine, method string, uri string) *httptest.ResponseRecorder { w := httptest.NewRecorder() req, err := http.NewRequest(method, uri, nil) server.ServeHTTP(w, req) if err != nil { panic(err) } return w }
func request(server *gin.Engine, options requestOptions) *httptest.ResponseRecorder { if options.Method == "" { options.Method = "GET" } w := httptest.NewRecorder() req, err := http.NewRequest(options.Method, options.URL, options.Body) if options.Headers != nil { for key, value := range options.Headers { req.Header.Set(key, value) } } server.ServeHTTP(w, req) if err != nil { panic(err) } return w }
recorder = httptest.NewRecorder() }) AfterEach(func() { databaseDB.Current.DropDatabase() }) Describe("GET /v1/parliamentarians", func() { BeforeEach(func() { request, _ = http.NewRequest("GET", "/v1/parliamentarians", nil) }) Context("when no parliamentarians exist", func() { It("returns a status code of 200", func() { r.ServeHTTP(recorder, request) Expect(recorder.Code).To(Equal(200)) }) It("returns a empty body", func() { r.ServeHTTP(recorder, request) Expect(recorder.Body.String()).To(Equal("{\"parliamentarians\":[]}\n")) }) }) Context("when parliamentarians exist", func() { BeforeEach(func() { databaseDB.Create(models.Parliamentarian{Name: "Jose"}) databaseDB.Create(models.Parliamentarian{Name: "Joao"}) })
chocStorage := storage.NewChocolateStorage() api.AddResource(model.User{}, resource.UserResource{ChocStorage: chocStorage, UserStorage: userStorage}) api.AddResource(model.Chocolate{}, resource.ChocolateResource{ChocStorage: chocStorage, UserStorage: userStorage}) }) BeforeEach(func() { log.SetOutput(ioutil.Discard) rec = httptest.NewRecorder() }) Context("CRUD Tests", func() { It("will create a new user", func() { reqBody := strings.NewReader(`{"data": [{"attributes": {"username": "******"}, "id": "1", "type": "users"}]}`) req, err := http.NewRequest("POST", "/api/users", reqBody) Expect(err).To(BeNil()) gg.ServeHTTP(rec, req) Expect(rec.Code).To(Equal(http.StatusCreated)) }) It("will find her", func() { expectedUser := ` { "data": { "attributes":{ "user-name":"Sansa Stark" }, "id":"1", "relationships":{ "sweets":{ "data":[],"links":{"related":"/api/users/1/sweets","self":"/api/users/1/relationships/sweets"}
// Clear the database after each test. //session.DB(dbName).DropDatabase() repo.Cleanup() }) Describe("GET /status", func() { // Set up a new GET request before every test // in this describe block. BeforeEach(func() { request, _ = http.NewRequest("GET", "/status", nil) }) Context("when service is running", func() { It("returns a status code of 200", func() { server.ServeHTTP(recorder, request) fmt.Printf("%v\n", recorder) Expect(recorder.Code).To(Equal(200)) }) }) }) Describe("GET /[email protected]", func() { var credentials *Credentials // Set up a new GET request before every test // in this describe block. BeforeEach(func() { request, _ = http.NewRequest("GET", "/api/[email protected]", nil) })
func request(engine *gin.Engine) func(*duktape.Context) int { return func(ctx *duktape.Context) int { res := map[string]interface{}{} req := map[string]interface{}{} // read first argument err := json.Unmarshal([]byte(ctx.GetString(1)), &req) if err != nil { res["code"] = http.StatusInternalServerError res["body"] = err.Error() return pushToCtxAndExit(ctx, res) } if ctx.GetTop() != 2 { res["code"] = http.StatusInternalServerError res["body"] = "request object not found" return pushToCtxAndExit(ctx, res) } method := req["method"].(string) url := req["url"].(string) // it needs to make crossdomain request from server side, if need if strings.HasPrefix(url, "//") || strings.HasPrefix(url, "http") { url = "/away?url=" + url } header := make(map[string]interface{}) if h, ok := req["headers"]; ok && h != nil { header = h.(map[string]interface{}) } var body io.Reader if b, ok := req["body"]; ok { if _b, ok := b.(string); ok { body = bytes.NewReader([]byte(_b)) } } response := httptest.NewRecorder() request, err := http.NewRequest( method, url, body, ) // TODO: copy cookie to request for k, v := range header { request.Header.Add(k, v.(string)) } request.Header.Add("X-Server-React", "true") // make request engine.ServeHTTP(response, request) rheaders := map[string][]string{} for k, v := range response.HeaderMap { rheaders[k] = v } res["headers"] = rheaders res["code"] = response.Code res["body"] = response.Body.String() if err != nil { res["code"] = http.StatusInternalServerError res["body"] = err.Error() } return pushToCtxAndExit(ctx, res) } }
func makeRequest(router *gin.Engine, httpMethod string, url string) { request, _ := http.NewRequest(httpMethod, url, nil) responseRecorder := httptest.NewRecorder() router.ServeHTTP(responseRecorder, request) }
func performRequest(router *gin.Engine, path string) *httptest.ResponseRecorder { w := httptest.NewRecorder() req, _ := http.NewRequest("GET", path, nil) router.ServeHTTP(w, req) return w }