aboutsummaryrefslogtreecommitdiff
path: root/lib/websocket/client_test.go
AgeCommit message (Collapse)Author
2025-01-23all: use for-range with numericShulhan
Go 1.22 now support for-range on numeric value.
2024-03-05all: comply with linter recommendations #3Shulhan
For HTTP server that use TLS, set the minimum TLS version and ReadHeaderTimeout to mitigate slowloris attack. For HTTP client or server that parameterize the use of InsecureSkipVerify, annotate the line with "nolint:gosec" to allow the code pass the check. Library that still use sha1, in example in DKIM and TOTP, skip the warnings by annotating the line with "nolint:gosec". A pointer variable now allocated their address before assigning its value. Any error that returned now wrapped using "%w". Also, replace error checking using [errors.Is] or [errors.As] instead of using equal or not-equal operators. In "lib/http", replace any usage of "math/rand" with "crypto/rand". Any call of [math/big.Rat.SetString] now annotated with "nolint:gosec" since its false positive, the issue has been fixed in Go >= 1.17.7. Any switch case that does not cover the rest of the possible values now handled by adding the cases or by replacing the "default" case with the rest of values.
2024-03-02all: move the repository to "git.sr.ht/~shulhan/pakakeh.go"Shulhan
There are several reasons that why we move from github.com. First, related to the name of package. We accidentally name the package with "share" a common word in English that does not reflect the content of repository. By moving to other repository, we can rename it to better and unique name, in this "pakakeh.go". Pakakeh is Minang word for tools, and ".go" suffix indicate that the repository related to Go programming language. Second, supporting open source. The new repository is hosted under sourcehut.org, the founder is known to support open source, and all their services are licensed under AGPL, unlike GitHub that are closed sources. Third, regarding GitHub CoPilot. The GitHub Terms of Service [1], allow any public content that are hosted there granted them to parse the content. On one side, GitHub helps and flourish the open source, but on another side have an issues regarding scraping the copyleft license [2]. [1]: https://docs.github.com/en/site-policy/github-terms/github-terms-of-service#4-license-grant-to-us [2]: https://githubcopilotinvestigation.com
2024-02-15all: set unused parameter to "_"Shulhan
2023-07-01lib/websocket: prefix the error with function or method namesShulhan
Adding prefix provide better way to locate and debug the error in the future.
2022-10-10lib/websocket: fix possible data race on ClientShulhan
The Client have method send that check if the underlying connection (conn) has been closed or not. Since the conn can be closed anytime, for example server send to the control CLOSE frame: recv -> handleFrame -> handleClose -> Quit we need to guard the conn with Mutex before calling send to prevent data race.
2022-09-15lib/websocket: fix possible race during testing ClientShulhan
During testing the Client we use the un-exported method send, while the test cases itself may close the connection and we did not guard it.
2022-07-29lib/websocket: fix possible data race on client testShulhan
We only do lock on exported methods, but when doing testing we call the send method without lock which cause read and write on Client.conn at the same time.
2022-06-09lib/websocket: refactoring codeShulhan
Replace assignment from using ":=" to use variable declaration with type. Rationale: clarity and minimize duplicate temporary variables with the same type.
2022-06-09lib/websocket: realign all struct to minimize allocationsShulhan
Changes, * Client: from 176 to 144 (-32 bytes) * ClientManager: from 64 to 40 (-24 bytes) * Request: from 88 to 72 (-16 bytes) * Response: from 40 to 24 (-16 bytes) * route: from 48 to 32 (-16 bytes) * Server: from 72 to 64 (-8 bytes) * ServerOptions: from 104 to 96 (s-8 bytes) Plus other structs in the tests.
2022-05-09all: reformat all codes using gofmt 1.19 (the Go tip)Shulhan
2021-10-08lib/websocket: fix test for client fragmentationShulhan
In the test cases we send two unmasked frames. Since the client to server must be masked, but in this test we did not mask it, when the server received first frame the connection may get closed before we can send the second unmasked frame. To detect this, we check if the error from send() is "broken pipe" or not. If its true then test case stopped.
2021-08-22lib/websocket: try to fix flaky test on clientShulhan
The following error thrown when running on Github Action using Ubuntu-latest and Go 1.16.3, client_test.go:472: write tcp 127.0.0.1:34460->127.0.0.1:9001: write: connection reset by peer This may be caused by using the same client connection on all test cases. We try to fix this by creating new client on each test cases.
2021-03-14all: refactoring the test.Assert and test.AssertBench signatureShulhan
Previously, the test.Assert and test.AssertBench functions has the boolean parameter to print the stack trace of test in case its not equal. Since this parameter is not mandatory and its usually always set to "true", we remove them from function signature to simplify the call to Assert and AssertBench.
2020-05-11websocket: allow "https" scheme on Client's EndpointShulhan
Previously, we only check for "wss" scheme to initialize the TLS configuration. At some point, user may have one single address to connect to HTTP and WebSocket server on the same domain, which require two addresses on their side. This commit relaxing the Endpoint rule when parsing WebSocket URI. Scheme "https" will be considered as "wss" or secure connection with TLS.
2020-04-29websocket: add method to gracefully Close the client connectionShulhan
The Close method send the control CLOSE frame to server and wait for the CLOSE response. This changes unexport the SendClose method.
2020-01-20websocket: fix data race on testShulhan
2019-11-19websocket: remove test for empty endpoint due to different formatShulhan
In Go tip, the format of error on empty URL is `parse ""` (with double quote), while in Go 1.13.4 there is no double quote on error message.
2019-11-05websocket: fix data race on testing FragmentationShulhan
2019-10-03websocket: remove the client Quit() statementShulhan
2019-09-05all: fix test for Go 1.13Shulhan
2019-03-17websocket: pass the instance of client to handlerShulhan
Since handler are created before creating a client and a handler may send a response to server after receiving a message from server, we pass the current client to handler as parameter.
2019-03-17websocket: unexport Frame Pack()Shulhan
The Frame Pack method is only used internally, implementor should use NewFrameXxx functions.
2019-03-17websocket: remove parameter randomMask from Frame.PackShulhan
The decision to randomize mask is now based on whether the maskKey length is equal to 4 or not. If its 4 the maskKey will not be randomized.
2019-03-17websocket: simplify handling chopped frameShulhan
Previously, we use two function to unpack frame: frameUnpack and continueUnpack, both of this functions actually use the same logic. This commit merge both functions into Frame's unpack() and which also make handleChopped() to be unused on both server and client.
2019-03-17websocket: rewrite client to use centralized handlerShulhan
Unlike HTTP client or other most commmon TCP oriented client, the WebSocket client is actually asynchronous or passive-active instead of synchronous. At any time client connection is open to server, client can receive a message broadcast from server. Case examples: if client send "A" to server, and expect that server response with "A+", server may send message "B" before sending "A+". Another case is when client connection is open, server may send "B" and "C" in any order without any request send by client previously. Due to this model, the way to handle response from server is centralized using handlers instead of using single send request-response.
2019-03-12websocket: export the opcode typeShulhan
The opcode type is required to create new frame and to get the opcode in continous frames.
2019-03-12websocket: unexport internal methods and remove unused methodsShulhan
Some of the methods in ClientManager and Frames should be only accessed by internal server, not by implementor.
2019-03-10websocket: add server handler for reserved control frameShulhan
The handler is called HandlerRsvControl that can be set before running the server. Default HandlerRsvControl is nil. Since the handler expect Frame as parameter, we need to adjust and add two methods to access Frame's internal fields and methods; which is exporting NewFrame function and Frame.Pack method, and adding method Opcode() and Payload() to access Frame's opcode and payload.
2019-03-10websocket: use defer to cleanup testShulhan
2019-03-07websocket: add mutex to prevent data race on send or receive on ClientShulhan
This is to enable single client to be used on multiple routines.
2019-03-07websocket: set the _testUID type to uint64 to minimize type convertionShulhan
2019-03-07websocket: add unit test for client SendClose and QuitShulhan
Also, make sure we close the client connection from unit test.
2019-03-07websocket: add unit test for client servePingShulhan
The test require to add a new handler on server to check received control PONG frame from client.
2019-03-07websocket: remove client connection stateShulhan
Previously, client connection state is used to disallow send or receive if client is not connected to remote server. This check also can be done by checking for nil on connection field.
2019-03-07websocket: add unit test for client SendBin() and SendPing()Shulhan
Changes caused by test: * handlePing become the internal field of client instead of method, so it can be replaced during test * add internal context ctxKeyFrame to save the expected frame in handler
2019-03-06websocket: prevent adding closed client connection back to epollShulhan
When server received unmasked frame or CLOSE control frame, the client connection will be closed. To prevent the connection to be added back to epoll we add a flag to indicate that connection will be closed.
2019-03-06websocket: refactoring handling fragmentation on serverShulhan
The temporary storage for continuous frame is moved from server to ClientManager, to prevent data race when modifying it and to allow one central function to clear it (when client closed). Another changes is on server handle parameter on HandleText and HandleBin. Previously, we pass the frame to handler, now we pass the payload since that is the real message the client send.
2019-03-05websocket: unexport the Frame Pack methodShulhan
The method Pack on single frame is not usable in the point of view of implementor, since we prefer NewFrameXxx to create frame by operation code.
2019-03-05websocket: replace concatBytes with our bytes.ConcatShulhan
function Concat in package bytes is more flexible, allow two or more slice of byte in parameters.
2019-03-05websocket: unexported the Frame fields Fin and MaskedShulhan
Continuing refactor, this commit unexport all fields in Frame to allow user focus working with method and packet, let the technical detail managed by library.
2019-03-04websocket: remove global variables for server control framesShulhan
This changes introduced new parameters isMasked and close code status to function NewFrameClose; and isMasked to function NewFramePing and NewFramePong. This commit also introduce new type for status code: CloseCode.
2019-03-04websocket: minimize global variables and unexport internal constantsShulhan
Most of the constants should not be used directly, as we prefer user to use the functions or methods based access.
2019-03-04websocket: refactoring client, add specific SendXxx methodsShulhan
The idea is to allow client to send message without worrying how to create frame manually. So, the new client provide the following methods: SendBin, SendClose, SendPing, SendPong, and SendText.
2019-03-04websocket: simplify client APIsShulhan
The client API now contains only NewClient() to create and connect to remote server, Send() to send package to remote server, and Recv() to receive package from remote. All fields and methods that is directly related to client usage are unexported.
2018-11-30all: minimize and suppress linter warnings for global variablesShulhan
2018-11-29all: fix warnings from lintersShulhan
2018-11-29all: fix warning from lintersShulhan
2018-07-06Add implementation of websocket server and client (RFC 6455)Shulhan