1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2025-07-02 04:18:38 -04:00

testing: Add common way to POST objects

This commit is contained in:
2017-10-08 21:18:30 -04:00
parent bd52df65cd
commit 2decf765ac
4 changed files with 88 additions and 65 deletions

View File

@ -2,6 +2,8 @@ package handlers_test
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/aclindsa/moneygo/internal/config"
"github.com/aclindsa/moneygo/internal/db"
"github.com/aclindsa/moneygo/internal/handlers"
@ -35,6 +37,43 @@ func PutForm(client *http.Client, url string, data url.Values) (*http.Response,
return client.Do(request)
}
type TransactType interface {
Read(string) error
}
func create(c *http.Client, input TransactType, output TransactType, urlsuffix, key string) error {
bytes, err := json.Marshal(input)
if err != nil {
return err
}
response, err := c.PostForm(server.URL+urlsuffix, url.Values{key: {string(bytes)}})
if err != nil {
return err
}
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
return err
}
var e handlers.Error
err = (&e).Read(string(body))
if err != nil {
return err
}
if e.ErrorId != 0 || len(e.ErrorString) != 0 {
return fmt.Errorf("Error when creating %s: %+v", key, e)
}
err = output.Read(string(body))
if err != nil {
return err
}
return nil
}
func RunWith(t *testing.T, d *TestData, fn TestDataFunc) {
testdata, err := d.Initialize()
if err != nil {