// PerformTestRequest does a HTTP request with test data on a specified endpoint func PerformTestRequest(route, endpoint, method string, expectedStatusCode int, urlParams url.Values, object interface{}, t *testing.T) *httptest.ResponseRecorder { generatedURL, err := generateEndpointURL(route, endpoint, urlParams) if err != nil { t.Error(err.Error()) } // Serialize object that will represent the request body // Do nothing if no object is specified var jsonData []byte if object != nil { jsonData, err = util.SerializeJSON(object) if err != nil { t.Fatal(err.Error()) } } req, err := http.NewRequest(method, generatedURL.String(), bytes.NewBuffer(jsonData)) if err != nil { t.Fatal(err.Error()) } rw := httptest.NewRecorder() httphandle.RequestHandler(rw, req) if rw.Code != expectedStatusCode { t.Fatal("Response assertion failed! Needed status:", expectedStatusCode, "Got:", rw.Code, "Message:", rw.Body.String()) } return rw }
// JSONResponse creates a Response from the api, containing a single entity encoded as JSON func JSONResponse(statusCode int, data interface{}) Response { jsonData, err := util.SerializeJSON(data) if err != nil { return InternalServerError(err) } return Response{ StatusCode: statusCode, Content: jsonData, } }
func saveUserTokens(storeLocation string, userID bson.ObjectId, tokens []string) error { userIndexFile := fileLocation(storeLocation, userID.Hex()) jsonData, err := util.SerializeJSON(tokens) if err != nil { return err } err = ioutil.WriteFile(userIndexFile, jsonData, os.ModeDevice) return err }
func generateGhostToken(session *cookies.Session) (string, error) { jsonToken, err := util.SerializeJSON(session) if err != nil { return err.Error(), err } encryptedToken, err := security.Encrypt(jsonToken) if err != nil { return err.Error(), err } ghostToken := util.Encode(encryptedToken) return string(ghostToken), nil }
// GeneratePrivateKey generates and prints in the terminal/log the byte array // containing a private RSA key serialized as a JSON value func GeneratePrivateKey(printInLog bool) []byte { priv, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { panic(err) } data, err := util.SerializeJSON(priv) if err != nil { panic(err) } if printInLog { log.Println(data) } return data }
// WriteCookie writes a cookie in the cookie store. If that cookie already exists, // it is overwritten func (store *FileCookieStore) WriteCookie(cookie *Session) error { fileName := fileLocation(store.location, cookie.Token) jsonData, err := util.SerializeJSON(cookie) if err != nil { return err } encryptedData, err := security.Encrypt(jsonData) if err != nil { return err } err = addUserToken(store.location, cookie.UserID, cookie.Token) if err != nil { return err } return ioutil.WriteFile(fileName, encryptedData, os.ModeDevice) }