|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "flag" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "io" |
| 11 | + "os" |
| 12 | + "strconv" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +type Api struct { |
| 17 | + BaseUrl string |
| 18 | +} |
| 19 | + |
| 20 | +func NewApi() *Api { |
| 21 | + s := Api{ BaseUrl: "http://localhost:4242/api" } |
| 22 | + return &s |
| 23 | +} |
| 24 | + |
| 25 | +func (a *Api) SendMessage(message *Message) error { |
| 26 | + endpoint := fmt.Sprintf("%s/message", a.BaseUrl) |
| 27 | + json_msg, err := json.Marshal(message) |
| 28 | + if err != nil { |
| 29 | + fmt.Printf("Failed to serialize message:\n%s", err) |
| 30 | + return err |
| 31 | + } |
| 32 | + |
| 33 | + req, err := http.NewRequest("POST", endpoint, bytes.NewReader(json_msg)) |
| 34 | + // req.Header.Set("X-Custom-Header", "myvalue") |
| 35 | + req.Header.Set("Content-Type", "application/json") |
| 36 | + |
| 37 | + client := &http.Client{} |
| 38 | + resp, err := client.Do(req) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + defer resp.Body.Close() |
| 43 | + |
| 44 | + fmt.Println("response Status:", resp.Status) |
| 45 | + fmt.Println("response Headers:", resp.Header) |
| 46 | + body, _ := io.ReadAll(resp.Body) |
| 47 | + fmt.Println("response Body:", string(body)) |
| 48 | + |
| 49 | + if resp.StatusCode != 200 { |
| 50 | + return errors.New("Failed to POST message to the API") |
| 51 | + } |
| 52 | + |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +type Message struct { |
| 57 | + Text string `json:"text"` |
| 58 | + Channel string `json:"channel"` |
| 59 | + Username string `json:"username"` |
| 60 | + UserId string `json:"userid"` |
| 61 | + Avatar string `json:"avatar"` |
| 62 | + Account string `json:"account"` |
| 63 | + Event string `json:"event"` |
| 64 | + Protocol string `json:"protocol"` |
| 65 | + Gateway string `json:"gateway"` |
| 66 | + ParentId string `json:"parent_id"` |
| 67 | + Timestamp string `json:"timestamp"` |
| 68 | + Id string `json:"id"` |
| 69 | + Extra map[string][]interface {} `json:"Extra"` |
| 70 | +} |
| 71 | + |
| 72 | +func newMessage(text string) *Message { |
| 73 | + s:= Message { |
| 74 | + Text: text, |
| 75 | + Channel: "testchannel", |
| 76 | + Username: "apitest", |
| 77 | + UserId: "apitest_id", |
| 78 | + Account: "api.test", |
| 79 | + Protocol: "api", |
| 80 | + Gateway: "test", |
| 81 | + Timestamp: "2019-01-09T22:53:51.618575236+01:00", |
| 82 | + } |
| 83 | + |
| 84 | + return &s |
| 85 | +} |
| 86 | + |
| 87 | +func scenario_outgoing_message(api *Api) error { |
| 88 | + err := api.SendMessage(newMessage("outgoing-message-test")) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +func apply_scenario(scenario string, api *Api) error { |
| 97 | + switch scenario { |
| 98 | + case "outgoing-message": |
| 99 | + return scenario_outgoing_message(api) |
| 100 | + default: |
| 101 | + fmt.Printf("ERROR: Unknown test scenario: %s\n", scenario) |
| 102 | + os.Exit(1) |
| 103 | + } |
| 104 | + |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func apply_scenario_timeout(scenario string, timeout int, api *Api) error { |
| 109 | + timeoutChan := make(chan error, 1) |
| 110 | + |
| 111 | + go func() { |
| 112 | + timeoutChan <- apply_scenario(scenario, api) |
| 113 | + }() |
| 114 | + |
| 115 | + select { |
| 116 | + case err := <-timeoutChan: |
| 117 | + if err != nil { |
| 118 | + return fmt.Errorf("ERROR: Scenario %s failed because of error:\n%s", scenario, err) |
| 119 | + } |
| 120 | + |
| 121 | + fmt.Printf("OK: Scenario %s", scenario) |
| 122 | + case <-time.After(time.Duration(timeout) * time.Second): |
| 123 | + return fmt.Errorf("ERROR: Scenario %s timeout after %d seconds", scenario, timeout) |
| 124 | + } |
| 125 | + |
| 126 | + return nil |
| 127 | +} |
| 128 | + |
| 129 | +func main() { |
| 130 | + flag.Parse() |
| 131 | + args := flag.Args() |
| 132 | + scenario := args[0] |
| 133 | + var timeout int |
| 134 | + if len(args) < 2 { |
| 135 | + timeout = 5 |
| 136 | + } else { |
| 137 | + timeout2, err := strconv.Atoi(args[1]) |
| 138 | + if err != nil { |
| 139 | + fmt.Printf("ERROR: Invalid timeout: %s\n", args[1]) |
| 140 | + os.Exit(1) |
| 141 | + } |
| 142 | + timeout = timeout2 |
| 143 | + } |
| 144 | + |
| 145 | + fmt.Println("Initializing API") |
| 146 | + api := NewApi() |
| 147 | + |
| 148 | + fmt.Printf("Running scenario %s (timeout=%ds)\n", scenario, timeout) |
| 149 | + |
| 150 | + err := apply_scenario_timeout(scenario, timeout, api) |
| 151 | + if err != nil { |
| 152 | + fmt.Println(err) |
| 153 | + os.Exit(1) |
| 154 | + } |
| 155 | +} |
0 commit comments