Implementation of route add

This commit is contained in:
Roberto Abdelkader Martínez Pérez
2019-10-03 20:56:40 +02:00
parent 9f7fc8ce62
commit 4357b1ad20
2 changed files with 37 additions and 29 deletions
+10 -23
View File
@@ -1,31 +1,18 @@
package client
import (
"errors"
"net/http"
"strings"
"bytes"
"encoding/json"
"github.com/BBVA/kapow/internal/http"
)
// AddRoute will add a new route in kapow
func AddRoute(host, path, method, entrypoint, command string) error {
reqData, err := http.NewRequest(
"PUT",
host+"/routes",
strings.NewReader(command),
)
if err != nil {
return err
}
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
url := host + "/routes"
body, _ := json.Marshal(map[string]string{
"method": method,
"url_pattern": path,
"entrypoint": entrypoint,
"command": command})
return http.Put(url, "application/json", bytes.NewReader(body), nil)
}
+27 -6
View File
@@ -1,11 +1,32 @@
package client
import "testing"
import (
"net/http"
"testing"
func TestInvalidURL(t *testing.T) {
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")
t.Fail()
gock "gopkg.in/h2non/gock.v1"
)
func TestSuccessOnCorrectRoute(t *testing.T) {
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")
}
}