ratchet_fixture/
lib.rs

1// Copyright 2015-2021 Swim Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15pub mod duplex {
16    use bytes::BytesMut;
17    use ratchet::{Extension, Role, WebSocketConfig};
18    use tokio::io::DuplexStream;
19
20    pub type MockWebSocket<E> = ratchet::WebSocket<DuplexStream, E>;
21
22    pub fn make_websocket<E>(stream: DuplexStream, role: Role, ext: Option<E>) -> MockWebSocket<E>
23    where
24        E: Extension,
25    {
26        ratchet::WebSocket::from_upgraded(
27            WebSocketConfig::default(),
28            stream,
29            ext,
30            BytesMut::new(),
31            role,
32        )
33    }
34
35    pub fn websocket_pair<L, R>(
36        left_ext: Option<L>,
37        right_ext: Option<R>,
38    ) -> (MockWebSocket<L>, MockWebSocket<R>)
39    where
40        L: Extension,
41        R: Extension,
42    {
43        let (tx, rx) = tokio::io::duplex(1024);
44        (
45            make_websocket(tx, Role::Client, left_ext),
46            make_websocket(rx, Role::Server, right_ext),
47        )
48    }
49
50    pub async fn websocket_for<E>(role: Role, ext: Option<E>) -> (MockWebSocket<E>, DuplexStream)
51    where
52        E: Extension,
53    {
54        let (tx, rx) = tokio::io::duplex(4096);
55        (
56            ratchet::WebSocket::from_upgraded(
57                WebSocketConfig::default(),
58                tx,
59                ext,
60                BytesMut::new(),
61                role,
62            ),
63            rx,
64        )
65    }
66}
67
68pub mod ratchet_failing_ext {
69    use bytes::BytesMut;
70    use ratchet::{
71        Extension, ExtensionDecoder, ExtensionEncoder, FrameHeader, RsvBits, SplittableExtension,
72    };
73    use std::error::Error;
74
75    #[derive(Clone, Debug)]
76    pub struct FailingExt<E>(pub E)
77    where
78        E: Error + Clone + Send + Sync + 'static;
79
80    impl<E> Extension for FailingExt<E>
81    where
82        E: Error + Clone + Send + Sync + 'static,
83    {
84        fn bits(&self) -> RsvBits {
85            RsvBits {
86                rsv1: false,
87                rsv2: false,
88                rsv3: false,
89            }
90        }
91    }
92
93    impl<E> ExtensionEncoder for FailingExt<E>
94    where
95        E: Error + Clone + Send + Sync + 'static,
96    {
97        type Error = E;
98
99        fn encode(
100            &mut self,
101            _payload: &mut BytesMut,
102            _header: &mut FrameHeader,
103        ) -> Result<(), Self::Error> {
104            Err(self.0.clone())
105        }
106    }
107
108    impl<E> ExtensionDecoder for FailingExt<E>
109    where
110        E: Error + Clone + Send + Sync + 'static,
111    {
112        type Error = E;
113
114        fn decode(
115            &mut self,
116            _payload: &mut BytesMut,
117            _header: &mut FrameHeader,
118        ) -> Result<(), Self::Error> {
119            Err(self.0.clone())
120        }
121    }
122
123    impl<E> SplittableExtension for FailingExt<E>
124    where
125        E: Error + Clone + Send + Sync + 'static,
126    {
127        type SplitEncoder = FailingExtEnc<E>;
128        type SplitDecoder = FailingExtDec<E>;
129
130        fn split(self) -> (Self::SplitEncoder, Self::SplitDecoder) {
131            let FailingExt(e) = self;
132            (FailingExtEnc(e.clone()), FailingExtDec(e))
133        }
134    }
135
136    pub struct FailingExtEnc<E>(pub E)
137    where
138        E: Error + Clone + Send + Sync + 'static;
139
140    impl<E> ExtensionEncoder for FailingExtEnc<E>
141    where
142        E: Error + Clone + Send + Sync + 'static,
143    {
144        type Error = E;
145
146        fn encode(
147            &mut self,
148            _payload: &mut BytesMut,
149            _header: &mut FrameHeader,
150        ) -> Result<(), Self::Error> {
151            Err(self.0.clone())
152        }
153    }
154
155    pub struct FailingExtDec<E>(pub E)
156    where
157        E: Error + Clone + Send + Sync + 'static;
158
159    impl<E> ExtensionDecoder for FailingExtDec<E>
160    where
161        E: Error + Clone + Send + Sync + 'static,
162    {
163        type Error = E;
164
165        fn decode(
166            &mut self,
167            _payload: &mut BytesMut,
168            _header: &mut FrameHeader,
169        ) -> Result<(), Self::Error> {
170            Err(self.0.clone())
171        }
172    }
173}