Implementation of route add
This commit is contained in:
@@ -1,31 +1,18 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"bytes"
|
||||||
"net/http"
|
"encoding/json"
|
||||||
"strings"
|
"github.com/BBVA/kapow/internal/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AddRoute will add a new route in kapow
|
// AddRoute will add a new route in kapow
|
||||||
func AddRoute(host, path, method, entrypoint, command string) error {
|
func AddRoute(host, path, method, entrypoint, command string) error {
|
||||||
reqData, err := http.NewRequest(
|
url := host + "/routes"
|
||||||
"PUT",
|
body, _ := json.Marshal(map[string]string{
|
||||||
host+"/routes",
|
"method": method,
|
||||||
strings.NewReader(command),
|
"url_pattern": path,
|
||||||
)
|
"entrypoint": entrypoint,
|
||||||
if err != nil {
|
"command": command})
|
||||||
return err
|
return http.Put(url, "application/json", bytes.NewReader(body), nil)
|
||||||
}
|
|
||||||
|
|
||||||
var client = new(http.Client)
|
|
||||||
res, err := client.Do(reqData)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if res.StatusCode < 200 || res.StatusCode >= 300 {
|
|
||||||
return errors.New(res.Status)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,32 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
|
||||||
func TestInvalidURL(t *testing.T) {
|
gock "gopkg.in/h2non/gock.v1"
|
||||||
err := AddRoute("http://localhost;8080", "/hi", "GET", "bash -c", "echo 'Hi' | kapow set /response/body")
|
)
|
||||||
if err == nil {
|
|
||||||
t.Error("expect to fail due invalid url")
|
func TestSuccessOnCorrectRoute(t *testing.T) {
|
||||||
t.Fail()
|
defer gock.Off()
|
||||||
|
gock.New("http://localhost").
|
||||||
|
Put("/routes").
|
||||||
|
MatchType("json").
|
||||||
|
JSON(map[string]string{
|
||||||
|
"method": "GET",
|
||||||
|
"url_pattern": "/hello",
|
||||||
|
"entrypoint": "",
|
||||||
|
"command": "echo Hello World | kapow set /response/body",
|
||||||
|
}).
|
||||||
|
Reply(http.StatusCreated).
|
||||||
|
JSON(map[string]string{})
|
||||||
|
|
||||||
|
err := AddRoute("http://localhost", "/hello", "GET", "", "echo Hello World | kapow set /response/body")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected error: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if gock.IsDone() == false {
|
||||||
|
t.Error("Expected endpoint call not made")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user