Skip to content

Commit

Permalink
fix: use crate version for user agent (#44)
Browse files Browse the repository at this point in the history
Uses the version from Cargo.toml to determine the default `User-Agent` version
  • Loading branch information
LeoBorai committed May 26, 2023
1 parent 149e1e3 commit 108ab70
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ target/

**/*.ipkg

log.txt

hub/**

# Development
log.txt
development-config.yml
2 changes: 1 addition & 1 deletion crates/http-sink/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::Duration;
use fluvio_connector_common::connector;
use url::Url;

const DEFAULT_USER_AGENT: &str = "fluvio/http-sink 0.1.0";
const DEFAULT_USER_AGENT: &str = concat!("fluvio/http-sink ", env!("CARGO_PKG_VERSION"));
const DEFAULT_HTTP_METHOD: &str = "POST";
const DEFAULT_HTTP_HEADERS: [&str; 1] = ["Content-Type: text/html"];

Expand Down
29 changes: 23 additions & 6 deletions crates/tiny-http-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,35 @@ use std::sync::{Arc, Mutex};

extern crate tiny_http;

#[derive(Clone, Debug, Default)]
pub struct RequestData {
#[allow(dead_code)]
payload: String,
#[allow(dead_code)]
user_agent: String,
}

#[derive(Clone, Debug, Default)]
struct State {
counter: Arc<Mutex<AtomicU32>>,
payload: Arc<Mutex<Vec<String>>>,
payload: Arc<Mutex<Vec<RequestData>>>,
}

impl State {
fn get_count(&self) -> u32 {
self.counter.lock().unwrap().get_mut().to_owned()
}

fn get_payload(&self) -> Vec<String> {
fn get_payload(&self) -> Vec<RequestData> {
self.payload.lock().unwrap().to_owned()
}

fn count(&self) -> u32 {
self.counter.lock().unwrap().fetch_add(1, Ordering::SeqCst)
}

fn append_payload(&self, payload: String) {
self.payload.lock().unwrap().push(payload);
fn append_payload(&self, data: RequestData) {
self.payload.lock().unwrap().push(data);
}
}

Expand Down Expand Up @@ -57,7 +65,7 @@ impl FileLogger {
String::from(s)
}

fn write_log(&mut self, counter: u32, payload: Vec<String>) {
fn write_log(&mut self, counter: u32, payload: Vec<RequestData>) {
writeln!(self.file, "Counter: {}\nPayloads: \n{:?}", counter, payload)
.expect("Failed to write to file");
}
Expand All @@ -74,9 +82,18 @@ fn main() {
for mut request in server.incoming_requests() {
let mut content = String::new();
request.as_reader().read_to_string(&mut content).unwrap();
let user_agent = request
.headers()
.iter()
.find(|h| h.field.equiv("user-agent"));

if !content.is_empty() {
state.append_payload(content.clone());
let data = RequestData {
payload: content.clone(),
user_agent: user_agent.map(|h| h.value.to_string()).unwrap_or_default(),
};

state.append_payload(data);
}

state.count();
Expand Down
20 changes: 18 additions & 2 deletions tests/integration-sends-data-via-post.bats
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ setup() {
cp ./tests/integration-sends-data-via-post.yaml $FILE

CONNECTOR=${UUID}-sends-data
VERSION=$(cat ./crates/http-sink/Connector.toml | grep "^version = " | cut -d"\"" -f2)
export VERSION=$(cat ./crates/http-sink/Connector.toml | grep "^version = " | cut -d"\"" -f2)
IPKG_NAME="http-sink-$VERSION.ipkg"
fluvio topic create $TOPIC

Expand All @@ -30,7 +30,7 @@ setup() {

teardown() {
fluvio topic delete $TOPIC
cdk deploy shutdown $CONNECTOR
cdk deploy shutdown --name $CONNECTOR
kill $MOCK_PID
}

Expand All @@ -49,3 +49,19 @@ teardown() {
cat ./$LOGGER_FILENAME | grep "California"
assert_success
}

@test "sends-user-agent-with-current-version" {
echo "Starting consumer on topic $TOPIC"
echo "Using connector $CONNECTOR"
sleep 45

echo "Produce \"North Carolina\" on $TOPIC"
echo "North Carolina" | fluvio produce $TOPIC

echo "Sleep to ensure record is processed"
sleep 25

echo "Contains User Agent with current version"
cat ./$LOGGER_FILENAME | grep "user_agent: \"fluvio/http-sink $VERSION\""
assert_success
}

0 comments on commit 108ab70

Please sign in to comment.