Add support for using testserver in doctests. (#218)

Doctests run against a normally-built copy of the crate, i.e. one
without #[cfg(test)] set, so we can't use the conditional compilation
feature.

Instead, define a static var that indicates whether the library is
running in test mode or not. For each doctest, insert a hidden call that
sets this var to true. Then, when ureq::agent() is called, it returns a
test_agent instead.

This required moving testserver out of the test mod and into src/, so
that it can be included unconditionally (i.e. when cfg(test) is false).

This PR converts one doctest as an example. If we land this PR, I'll
send a followup to convert the rest.
This commit is contained in:
Jacob Hoffman-Andrews
2020-11-13 10:40:16 -08:00
committed by GitHub
parent a0b901f35b
commit acc36ac370
8 changed files with 177 additions and 132 deletions

View File

@@ -1,4 +1,4 @@
use crate::test::testserver::*;
use crate::testserver::*;
use std::io::{self, Write};
use std::net::TcpStream;
use std::thread;