-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reviewed By: kassens Differential Revision: D21559109 fbshipit-source-id: 2e40a38f030ddda5dd481d6c0e76802c19eeb571
- Loading branch information
1 parent
c56b89d
commit 2a3a802
Showing
3 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
/// Escape graphql text | ||
pub fn escape(text: &str, output: &mut String) { | ||
for char in text.chars() { | ||
match char { | ||
'\n' => output.push_str("\\n"), | ||
'"' => output.push_str("\\\""), | ||
'\\' => output.push_str("\\\\"), | ||
'\t' => output.push_str("\\t"), | ||
'\r' => output.push_str("\\r"), | ||
_ => output.push(char), | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_escape() { | ||
let input = r#"query Query( | ||
$unixname: String! | ||
$passcode: String! | ||
) @owner(oncall: "oncall") { | ||
auth_2fa { | ||
is_auth(unixname: $unixname, passcode: $passcode) | ||
} | ||
} | ||
"#; | ||
let expected = r#"query Query(\n $unixname: String!\n $passcode: String!\n) @owner(oncall: \"oncall\") {\n auth_2fa {\n is_auth(unixname: $unixname, passcode: $passcode)\n }\n}\n"#; | ||
let mut output = String::new(); | ||
escape(input, &mut output); | ||
assert_eq!(output, expected) | ||
} |