DetectHTTPStreamConfig detects the streaming mode of an HTTP response based on content type and other factors.
(tc *models.TestCase, resp *http.Response)
| 876 | |
| 877 | // DetectHTTPStreamConfig detects the streaming mode of an HTTP response based on content type and other factors. |
| 878 | func DetectHTTPStreamConfig(tc *models.TestCase, resp *http.Response) HTTPStreamConfig { |
| 879 | contentType := "" |
| 880 | if resp != nil { |
| 881 | contentType = resp.Header.Get("Content-Type") |
| 882 | } |
| 883 | if contentType == "" && tc != nil { |
| 884 | contentType = getHeaderValueCaseInsensitive(tc.HTTPResp.Header, "Content-Type") |
| 885 | } |
| 886 | |
| 887 | mediaType := "" |
| 888 | params := map[string]string{} |
| 889 | if contentType != "" { |
| 890 | parsedType, parsedParams, err := mime.ParseMediaType(contentType) |
| 891 | if err == nil { |
| 892 | mediaType = strings.ToLower(strings.TrimSpace(parsedType)) |
| 893 | params = parsedParams |
| 894 | } else { |
| 895 | mediaType = strings.ToLower(strings.TrimSpace(contentType)) |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | switch mediaType { |
| 900 | case "text/event-stream": |
| 901 | if isSSETestCase(tc, resp) { |
| 902 | return HTTPStreamConfig{Mode: HTTPStreamModeSSE} |
| 903 | } |
| 904 | return HTTPStreamConfig{Mode: HTTPStreamModePlainText} |
| 905 | case "application/x-ndjson", "application/ndjson": |
| 906 | return HTTPStreamConfig{Mode: HTTPStreamModeNDJSON} |
| 907 | case "multipart/x-mixed-replace", "multipart/mixed": |
| 908 | boundary := strings.TrimSpace(params["boundary"]) |
| 909 | if boundary == "" && tc != nil { |
| 910 | boundary = boundaryFromContentTypeHeader(getHeaderValueCaseInsensitive(tc.HTTPResp.Header, "Content-Type")) |
| 911 | } |
| 912 | if boundary != "" { |
| 913 | return HTTPStreamConfig{Mode: HTTPStreamModeMultipart, Boundary: boundary} |
| 914 | } |
| 915 | case "text/plain": |
| 916 | if tc != nil && len(tc.HTTPResp.StreamBody) > 0 { |
| 917 | return HTTPStreamConfig{Mode: HTTPStreamModePlainText} |
| 918 | } |
| 919 | if isLikelyStreamingHTTPResponse(tc, resp) { |
| 920 | return HTTPStreamConfig{Mode: HTTPStreamModePlainText} |
| 921 | } |
| 922 | case "application/octet-stream": |
| 923 | if tc != nil && len(tc.HTTPResp.StreamBody) > 0 { |
| 924 | return HTTPStreamConfig{Mode: HTTPStreamModeBinary} |
| 925 | } |
| 926 | if isLikelyStreamingHTTPResponse(tc, resp) { |
| 927 | return HTTPStreamConfig{Mode: HTTPStreamModeBinary} |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | return HTTPStreamConfig{Mode: HTTPStreamModeNone} |
| 932 | } |
| 933 | |
| 934 | // IsHTTPStreamingTestCase returns true if the testcase response is identified as a stream format |
| 935 | // supported by replay-time incremental validators. |
no test coverage detected