Skip to content

Instantly share code, notes, and snippets.

@samdfonseca
Last active August 16, 2016 22:04
Show Gist options
  • Save samdfonseca/b3f2cfa5b2a746d87680895f015bd0c9 to your computer and use it in GitHub Desktop.
Save samdfonseca/b3f2cfa5b2a746d87680895f015bd0c9 to your computer and use it in GitHub Desktop.
tpg-mock-api

tpg-mock-api is a mock server for the TPG DRVS API for testing specific scenarios. You specify the phase1 response data, and phase2 request data through the img field on the initial request. See success.jpg and error.jpg for examples.

package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
)
func SendResults(reqUrl string, body []byte, delay string) {
delayDuration, err := time.ParseDuration(delay)
if err != nil {
log.Println(err)
return
}
log.Printf("Sending results to %s after %s delay", reqUrl, delay)
time.Sleep(delayDuration)
log.Printf("Sending results to %s", reqUrl)
reqBody := bytes.NewBuffer(body)
resp, err := http.DefaultClient.Post(reqUrl, "application/json", reqBody)
if err != nil {
log.Printf("POST to %s failed: %s", reqUrl, err)
}
if resp.StatusCode >= 400 {
log.Printf("POST to %s failed: %s %s", reqUrl, resp.StatusCode, resp.Status)
}
}
func main() {
http.HandleFunc("/drvsapi/imgpost/", func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
imgBytes := []byte(r.PostFormValue("img"))
processId := r.PostFormValue("process_id")
var imgData map[string]map[string]interface{}
err := json.Unmarshal(imgBytes, &imgData)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
imgData["phase1"]["process_id"] = processId
imgData["phase2"]["process_id"] = processId
respBody, err := json.Marshal(imgData["phase1"])
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write(respBody)
if !imgData["phase1"]["success"].(bool) {
return
}
delay := string(imgData["phase2"]["delay"].(string))
delete(imgData["phase2"], "delay")
resultsUrl := os.Getenv("TPG_MOCK_RESULTS_URL")
if resultsUrl == "" {
resultsUrl = "http://localhost:8000"
}
url := fmt.Sprintf("%s/%s", resultsUrl, processId)
resReqBody, err := json.Marshal(imgData["phase2"])
if err != nil {
log.Println(err)
return
}
go SendResults(url, resReqBody, delay)
})
port := os.Getenv("TPG_MOCK_PORT")
if port == "" {
port = "8080"
}
log.Printf("Listening on 0.0.0.0:%s", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}
{
"phase1": {
"error": 1,
"external_id": "abc123",
"msg": "bad_ip",
"success": false
}
}
{
"phase1": {
"error": 2,
"external_id": "abc123",
"msg": "bad_program",
"success": false
}
}
{
"phase1": {
"error": 3,
"external_id": "abc123",
"msg": "bad_process_id",
"success": false
}
}
{
"phase1": {
"error": 4,
"external_id": "abc123",
"msg": "bad_img_size",
"success": false
}
}
{
"phase1": {
"error": 5,
"external_id": "abc123",
"msg": "bad_img_type",
"success": false
}
}
{
"phase1": {
"error": 6,
"external_id": "abc123",
"msg": "pending submission",
"success": false
}
}
{
"phase1": {
"error": 7,
"external_id": "abc123",
"msg": "user limit reached",
"success": false
}
}
{
"phase1": {
"error": 8,
"external_id": "abc123",
"msg": "general system fail",
"success": false
}
}
{
"phase1": {
"error": 0,
"external_id": "abc123",
"msg": "accepted for processing",
"success": true
},
"phase2": {
"delay": "10s",
"success": false,
"error": 1,
"error_message": "receipt can not be read"
}
}
{
"phase1": {
"error": 0,
"external_id": "abc123",
"msg": "accepted for processing",
"success": true
},
"phase2": {
"delay": "10s",
"success": false,
"error": 2,
"error_message": "invalid purchase date"
}
}
{
"phase1": {
"error": 0,
"external_id": "abc123",
"msg": "accepted for processing",
"success": true
},
"phase2": {
"delay": "10s",
"success": false,
"error": 3,
"error_message": "products fail"
}
}
{
"phase1": {
"error": 0,
"external_id": "abc123",
"msg": "accepted for processing",
"success": true
},
"phase2": {
"delay": "10s",
"success": false,
"error": 4,
"error_message": "duplicate submission"
}
}
{
"phase1": {
"error": 0,
"external_id": "abc123",
"msg": "accepted for processing",
"success": true
},
"phase2": {
"delay": "10s",
"success": false,
"error": 5,
"error_message": "invalid store"
}
}
{
"phase1": {
"error": 0,
"external_id": "abc123",
"msg": "accepted for processing",
"success": true
},
"phase2": {
"delay": "10s",
"success": false,
"error": 6,
"error_message": "defer to customer service"
}
}
{
"phase1": {
"error": 0,
"external_id": "abc123",
"msg": "accepted for processing",
"success": true
},
"phase2": {
"delay": "10s",
"success": false,
"error": 8,
"error_message": "potential fraud detection"
}
}
{
"phase1": {
"error": 0,
"external_id": "abc123",
"msg": "accepted for processing",
"success": true
},
"phase2": {
"delay": "60s",
"success": true,
"data": {
"products": "sku1, sku2, sku3",
"products_total": "20.00",
"receipt_total": "40.00",
"store": "Walmart"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment