(w http.ResponseWriter, r *http.Request)
| 1101 | var patt = regexp.MustCompile(`(\d+)$`) |
| 1102 | |
| 1103 | func fakeCHHandler(w http.ResponseWriter, r *http.Request) { |
| 1104 | query, err := getFullQuery(r) |
| 1105 | if err != nil { |
| 1106 | w.WriteHeader(http.StatusInternalServerError) |
| 1107 | fmt.Fprintf(w, "error while reading query: %s", err) |
| 1108 | return |
| 1109 | } |
| 1110 | if len(query) == 0 && r.Method != http.MethodGet { |
| 1111 | w.WriteHeader(http.StatusInternalServerError) |
| 1112 | fmt.Fprint(w, "got empty query for non-GET request") |
| 1113 | return |
| 1114 | } |
| 1115 | defer func() { |
| 1116 | if f, ok := w.(http.Flusher); ok { |
| 1117 | f.Flush() |
| 1118 | } |
| 1119 | }() |
| 1120 | q := string(query) |
| 1121 | switch { |
| 1122 | case q == "SELECT ERROR": |
| 1123 | w.WriteHeader(http.StatusInternalServerError) |
| 1124 | fmt.Fprint(w, "DB::Exception\n") |
| 1125 | case q == "SELECT RECOVERABLE-ERROR": |
| 1126 | println("called clickhouse recoverable") |
| 1127 | w.WriteHeader(http.StatusServiceUnavailable) |
| 1128 | fmt.Fprint(w, "DB::Unavailable\n") |
| 1129 | case q == "SELECT SLEEP": |
| 1130 | w.WriteHeader(http.StatusOK) |
| 1131 | fmt.Fprint(w, "foo") |
| 1132 | fakeCHState.sleep() |
| 1133 | fmt.Fprint(w, "bar") |
| 1134 | |
| 1135 | case strings.Contains(q, "SELECT SLEEP"): |
| 1136 | numberStrings := patt.FindAllStringSubmatch(q, -1) |
| 1137 | if len(numberStrings) == 0 { |
| 1138 | panic("couldn't extract number from sleep call to clickhouse mock") |
| 1139 | } |
| 1140 | nr, err := strconv.Atoi(numberStrings[0][1]) |
| 1141 | if err != nil { |
| 1142 | panic(err) |
| 1143 | } |
| 1144 | |
| 1145 | duration, err := time.ParseDuration(fmt.Sprintf("%vms", nr)) |
| 1146 | if err != nil { |
| 1147 | panic(err) |
| 1148 | } |
| 1149 | time.Sleep(duration) |
| 1150 | w.WriteHeader(http.StatusOK) |
| 1151 | fmt.Fprint(w, "success mate") //nolint |
| 1152 | case q == "SELECT 1 FORMAT TabSeparatedWithNamesAndTypes": |
| 1153 | w.WriteHeader(http.StatusOK) |
| 1154 | w.Write(bytesWithInvalidUTFPairs) |
| 1155 | case q == "SELECT MaxPayloadSize": |
| 1156 | w.WriteHeader(http.StatusOK) |
| 1157 | |
| 1158 | // generate 10M payload size |
| 1159 | b := make([]byte, 10485760) |
| 1160 | w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
nothing calls this directly
no test coverage detected