{"modulePath":"github.com/linuxsuren/api-testing","version":"v0.0.21","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package util\n\nCopyright 2023-2025 API Testing Authors.\n\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at\n\n\thttp://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n\n### Package util provides a set of common functions {#hdr-Package_util_provides_a_set_of_common_functions}\n\nCopyright 2023 API Testing Authors.\n\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at\n\n\thttp://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n\nCopyright 2023 API Testing Authors.\n\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at\n\n\thttp://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n\nCopyright 2023 API Testing Authors.\n\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at\n\n\thttp://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n\nCopyright 2023 API Testing Authors.\n\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at\n\n\thttp://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n\nCopyright 2023 API Testing Authors.\n\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at\n\n\thttp://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n\nCopyright 2024 API Testing Authors.\n\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at\n\n\thttp://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n\nCopyright 2023 API Testing Authors.\n\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at\n\n\thttp://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n\nPackage util provides utilities related to randomization.\n\nCopyright 2023 API Testing Authors.\n\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at\n\n\thttp://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n\n## Constants\n\n```go\nconst (\n\tImageBase64Prefix  = \"data:image/png;base64,\"\n\tPDFBase64Prefix    = \"data:application/pdf;base64,\"\n\tZIPBase64Prefix    = \"data:application/zip;base64,\"\n\tBinaryBase64Prefix = \"data:application/octet-stream;base64,\"\n)\n```\n\n```go\nconst (\n\tContentType        = \"Content-Type\"\n\tContentLength      = \"Content-Length\"\n\tContentDisposition = \"Content-Disposition\"\n\tMultiPartFormData  = \"multipart/form-data\"\n\tForm               = \"application/x-www-form-urlencoded\"\n\tJSON               = \"application/json\"\n\tJavaScript         = \"application/javascript\"\n\tOCIImageIndex      = \"application/vnd.oci.image.index.v1+json\"\n\tYAML               = \"application/yaml\"\n\tZIP                = \"application/zip\"\n\tXML                = \"application/xml\"\n\tOctetStream        = \"application/octet-stream\"\n\tImage              = \"image/jpeg\"\n\tImagePNG           = \"image/png\"\n\tSVG                = \"image/svg+xml\"\n\tPlain              = \"text/plain\"\n\tCSS                = \"text/css\"\n\tHTML               = \"text/html\"\n\tAuthorization      = \"Authorization\"\n)\n```\nContentType is the HTTP header key\n\n```go\nconst PasswordPlaceholder = \"******\"\n```\n\n## Functions\n\n```go\nfunc EmptyThenDefault(val, defVal string) string\n```\nEmptyThenDefault return the default value if the val is empty\n\n```go\nfunc ErrorWrap(err error, msg string, args ...interface{}) error\n```\n\n```go\nfunc Expand(text string) (result []string)\n```\nExpand the text with brace syntax. Such as: /home/{good,bad} -\u003e \\[/home/good, /home/bad]\n\n```go\nfunc GetDefaultCachedHTTPClient() *http.Client\n```\n\n```go\nfunc GetFirstHeaderValue(header http.Header, key string) (val string)\n```\nGetFirstHeaderValue retursn the first value of the header\n\n```go\nfunc GetPort(listener net.Listener) string\n```\nGetPort returns the port of the listener\n\n```go\nfunc IgnoreErrServerClosed(err error) error\n```\nIgnoreErrServerClosed ignores ErrServerClosed\n\n```go\nfunc Keys[T interface{}](data map[string]T) (keys []string)\n```\nKeys returns a list of keys\n\n```go\nfunc LoadProtoFiles(protoFile string) (targetProtoFile string, importPath []string, protoParentDir string, err error)\n```\n\n```go\nfunc MakeSureNotNil[T any](inter T) T\n```\nMakeSureNotNil makes sure the parameter is not nil\n\n```go\nfunc OKOrErrorMessage(err error) string\n```\nOKOrErrorMessage returns OK or error message\n\n```go\nfunc OrErrorMessage(err error, message string) string\n```\nOrErrorMessage returns error message or message\n\n```go\nfunc PathExists(path string) (ok bool, err error)\n```\nPathExists checks if the target path exist or not\n\n```go\nfunc RemoeEmptyFromSlice(items []string) []string\n```\n\n```go\nfunc String(n int) string\n```\nString generates a random alphanumeric string, without vowels, which is n characters long.  This will panic if n is less than zero. How the random string is created: - we generate random int63's - from each int63, we are extracting multiple random letters by bit-shifting and masking - if some index is out of range of alphanums we neglect it (unlikely to happen multiple times in a row)\n\n```go\nfunc TlsAwareHTTPClient(insecure bool) *http.Client\n```\n\n```go\nfunc WriteAsJSON(obj interface{}, w http.ResponseWriter) (n int, err error)\n```\n\n```go\nfunc ZeroThenDefault(val, defVal int) int\n```\nZeroThenDefault return the default value if the val is zero\n\n","path":"github.com/linuxsuren/api-testing/pkg/util","name":"util","synopsis":"","isRedistributable":true}
