Created
April 12, 2017 00:21
-
-
Save matiasinsaurralde/c893510d23a8f6f7fdac7cf735df9260 to your computer and use it in GitHub Desktop.
connector_test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package connector_test | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"testing" | |
"time" | |
"bytes" | |
"github.com/TykTechnologies/tyk-cf-connector/connector" | |
) | |
const ( | |
upstreamHTTPListen = "127.0.0.1:8000" | |
connectorHTTPListen = "127.0.0.1:7000" | |
) | |
type RequestObject struct { | |
Method string `json:"method"` | |
URL string `json:"url"` | |
Headers map[string][]string `json:"headers"` | |
Body string `json:"body"` | |
} | |
func testHTTPHandler() http.Handler { | |
httpError := func(w http.ResponseWriter, status int) { | |
http.Error(w, http.StatusText(status), status) | |
} | |
writeDetails := func(w http.ResponseWriter, r *http.Request) { | |
body, _ := ioutil.ReadAll(r.Body) | |
o := RequestObject{ | |
Method: r.Method, | |
URL: r.URL.String(), | |
Headers: r.Header, | |
Body: string(body), | |
} | |
err := json.NewEncoder(w).Encode(o) | |
if err != nil { | |
httpError(w, http.StatusInternalServerError) | |
} | |
} | |
handleMethod := func(method string) http.HandlerFunc { | |
return func(w http.ResponseWriter, r *http.Request) { | |
if method != "" && r.Method != method { | |
httpError(w, http.StatusMethodNotAllowed) | |
} else { | |
writeDetails(w, r) | |
} | |
} | |
} | |
mux := http.NewServeMux() | |
mux.HandleFunc("/", handleMethod("")) | |
mux.HandleFunc("/get", handleMethod("GET")) | |
mux.HandleFunc("/post", handleMethod("POST")) | |
mux.HandleFunc("/put", handleMethod("PUT")) | |
return mux | |
} | |
func init() { | |
testServer := &http.Server{ | |
Addr: upstreamHTTPListen, | |
Handler: testHTTPHandler(), | |
ReadTimeout: 1 * time.Second, | |
WriteTimeout: 1 * time.Second, | |
MaxHeaderBytes: 1 << 20, | |
} | |
connectorServer := &http.Server{ | |
Addr: connectorHTTPListen, | |
Handler: connector.Connector(), | |
ReadTimeout: 1 * time.Second, | |
WriteTimeout: 1 * time.Second, | |
MaxHeaderBytes: 1 << 20, | |
} | |
go testServer.ListenAndServe() | |
go connectorServer.ListenAndServe() | |
} | |
func TestConnector(t *testing.T) { | |
var ( | |
client = http.Client{} | |
testData = "testdata" | |
testMethod = "POST" | |
targetURL = fmt.Sprintf("http://%s/", upstreamHTTPListen) | |
connectorURL = fmt.Sprintf("http://%s/", connectorHTTPListen) | |
) | |
upstreamRequest, err := http.NewRequest(testMethod, targetURL, bytes.NewBufferString(testData)) | |
if err != nil { | |
t.Fatal("Couldn't initialize upstream request: ", err) | |
} | |
upstreamResponse, err := client.Do(upstreamRequest) | |
if err != nil { | |
t.Fatal("Couldn't send upstream response: ", err) | |
} | |
defer upstreamResponse.Body.Close() | |
upstreamResponseBody, err := ioutil.ReadAll(upstreamResponse.Body) | |
if err != nil { | |
t.Fatal("Couldn't read upstream response: ", err) | |
} | |
var upstreamRequestObject, connectorRequestObject RequestObject | |
err = json.Unmarshal(upstreamResponseBody, &upstreamRequestObject) | |
if err != nil { | |
t.Fatal("Couldn't unmarshal upstream response: ", err) | |
} | |
connectorRequest, err := http.NewRequest(testMethod, connectorURL, bytes.NewBufferString(testData)) | |
if err != nil { | |
t.Fatal("Couldn't initialize connector request: ", err) | |
} | |
connectorRequest.Header.Add(connector.CFURLHeader, targetURL) | |
connectorResponse, err := client.Do(connectorRequest) | |
if err != nil { | |
t.Fatal("Couldn't send connector response: ", err) | |
} | |
defer connectorResponse.Body.Close() | |
connectorResponseBody, err := ioutil.ReadAll(connectorResponse.Body) | |
if err != nil { | |
t.Fatal("Couldn't read connector response: ", err) | |
} | |
err = json.Unmarshal(connectorResponseBody, &connectorRequestObject) | |
if err != nil { | |
t.Fatal("Couldn't unmarshal connector response: ", err) | |
} | |
cfHeader := connectorRequestObject.Headers[connector.CFURLHeader] | |
if len(cfHeader) == 0 { | |
t.Fatal("CF header is empty") | |
} | |
cfHeaderValue := cfHeader[0] | |
if cfHeaderValue != targetURL { | |
t.Fatal("CF header doesn't contain the expected target URL") | |
} | |
if upstreamRequestObject.Method != connectorRequestObject.Method { | |
t.Fatal("Request method doesn't match") | |
} | |
if upstreamRequestObject.URL != connectorRequestObject.URL { | |
t.Fatal("Request URL doesn't match") | |
} | |
if upstreamRequestObject.Body != connectorRequestObject.Body { | |
t.Fatal("Request body doesn't match") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment