Skip to content

Commit

Permalink
support mockito
Browse files Browse the repository at this point in the history
  • Loading branch information
Kouhei Aoyagi committed Mar 9, 2024
1 parent 783356c commit dd13f95
Show file tree
Hide file tree
Showing 167 changed files with 599 additions and 343 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Changes

### v0.12.0 (2024/03/09)
* Supported mocks. For example, mockito.

### v0.11.0 (2024/03/08)
* add media_source_tweet_id in attachments
* remove rate_limit
Expand Down
132 changes: 131 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "twapi-v2"
version = "0.11.0"
version = "0.12.0"
authors = ["aoyagikouhei <aoyagi.kouhei@gmail.com>"]
license = "MIT"
edition = "2021"
Expand Down Expand Up @@ -35,8 +35,9 @@ oauth10a = ["base64", "hmac", "rand", "sha1", "url"]
models = []

[dev-dependencies]
tokio = { version = "1", features = ["macros"] }
anyhow = "1"
mockito = "1.4"
tokio = { version = "1", features = ["macros"] }

[package.metadata.docs.rs]
all-features = true
Expand Down
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Twitter API v2 library.
- Optional OAuth with web example
- Optional v1 to v2 parser
- Streaming example
- Supported mocks. For example, mockito.
- **Experimental** type support.

## Features
Expand Down Expand Up @@ -116,4 +117,36 @@ http://localhost:3000/
```
cd examples/streaming
BEARER_CODE=XXXXX cargo run
```

### Mock(Use mockito)
```rust
#[tokio::test]
async fn test_mock_get_2_tweets_search_recent_oauth() -> Result<()> {
let mut server = Server::new_async().await;
api::setup_prefix_url(&server.url());

let mock = server
.mock("GET", "/2/tweets/search/recent")
.match_query(mockito::Matcher::Any)
.with_status(200)
.with_header("content-type", "application/json")
.with_body("{ \"origin\": \"0.0.0.0\" }")
.create_async()
.await;

let auth = OAuthAuthentication::new(
std::env::var("CONSUMER_KEY").unwrap_or_default(),
std::env::var("CONSUMER_SECRET").unwrap_or_default(),
std::env::var("ACCESS_KEY").unwrap_or_default(),
std::env::var("ACCESS_SECRET").unwrap_or_default(),
);
let builder = get_2_tweets_search_recent::Api::open("東京")
.max_results(10)
.build(&auth);
let (res, _headers) = execute_twitter::<get_2_tweets_search_recent::Response>(builder).await?;
assert_eq!(res.extra.get("origin"), Some(&json!("0.0.0.0")));
mock.assert();
Ok(())
}
```
2 changes: 1 addition & 1 deletion examples/oauth-web/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/streaming/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions maker/api.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::collections::HashSet;
<% end %><% if fields.present? %>use crate::fields::{<%= fields.map{|it| "#{it[:name].make_field()}::#{it[:name].ucc}"}.join(", ") %>};
<% end %><% if refs.present? %>use crate::responses::{<%= refs.map{|it| "#{it}::#{it.ucc}"}.join(", ") %>};
<% end %>use reqwest::RequestBuilder;
use crate::{error::Error, headers::Headers, api::{execute_twitter, Authentication}};
use crate::{error::Error, headers::Headers, api::{execute_twitter, Authentication, make_url}};

const URL: &str = "<%= yml[:url] %>";

Expand All @@ -21,7 +21,7 @@ impl Api {
pub fn build(self, authentication: &impl Authentication) -> RequestBuilder {
<%= parameters %>
let client = reqwest::Client::new();
let url = URL<%= paths.empty? ? ".to_string()" : paths.map{|it| ".replace(\":#{it[:name].make_field}\", &self.#{it[:name].make_field})"}.join("") %>;
let url = make_url(URL<%= paths.empty? ? "" : paths.map{|it| ".replace(\":#{it[:name].make_field}\", &self.#{it[:name].make_field})"}.join("") %>);
let builder = client
.<%= yml[:method] %>(&url)<% if queries.present? %>
.query(&query_parameters)<% end %><% if form.present? %>
Expand Down
2 changes: 1 addition & 1 deletion maker/api/delete_2_lists_id.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---

url: https://api.twitter.com/2/lists/:id
url: /2/lists/:id
method: delete
paths:
- name: id
Expand Down
2 changes: 1 addition & 1 deletion maker/api/delete_2_lists_id_members_user_id.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---

url: https://api.twitter.com/2/lists/:id/members/:user_id
url: /2/lists/:id/members/:user_id
method: delete
paths:
- name: id
Expand Down
2 changes: 1 addition & 1 deletion maker/api/delete_2_tweets_id.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---

url: https://api.twitter.com/2/tweets/:id
url: /2/tweets/:id
method: delete
paths:
- name: id
Expand Down
2 changes: 1 addition & 1 deletion maker/api/delete_2_users_id_bookmarks_tweet_id.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---

url: https://api.twitter.com/2/users/:id/bookmarks/:tweet_id
url: /2/users/:id/bookmarks/:tweet_id
method: delete
paths:
- name: id
Expand Down
2 changes: 1 addition & 1 deletion maker/api/delete_2_users_id_followed_lists_list_id.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---

url: https://api.twitter.com/2/users/:id/followed_lists/:list_id
url: /2/users/:id/followed_lists/:list_id
method: delete
paths:
- name: id
Expand Down
2 changes: 1 addition & 1 deletion maker/api/delete_2_users_id_likes_tweet_id.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---

url: https://api.twitter.com/2/users/:id/likes/:tweet_id
url: /2/users/:id/likes/:tweet_id
method: delete
paths:
- name: id
Expand Down
2 changes: 1 addition & 1 deletion maker/api/delete_2_users_id_pinned_lists.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---

url: https://api.twitter.com/2/users/:id/pinned_lists/:list_id
url: /2/users/:id/pinned_lists/:list_id
method: delete
paths:
- name: id
Expand Down
2 changes: 1 addition & 1 deletion maker/api/delete_2_users_id_retweets_source_tweet_id.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---

url: https://api.twitter.com/2/users/:id/retweets/:source_tweet_id
url: /2/users/:id/retweets/:source_tweet_id
method: delete
paths:
- name: id
Expand Down

0 comments on commit dd13f95

Please sign in to comment.